Alternatively,
f = cell(3,1); % create a cell array
% initialize
f{1} = @(t) t^2;
f{2} = @(t) cos(2*t);
f{3} = @(t) 4*(t^3);
% access properties
size(f)(1); % access the number of functions
f{1} % access the first function
f{2}(17) % evaluate the second function at x = 17
A cell array is a data type with indexed data containers called cells, where each cell can contain any type of data.
Documentation: https://www.mathworks.com/help/matlab/ref/cell.html
Alternatively, In Octave:
f = cell(3,1); # create a cell array
# initialize
f(1) = @(t) t^2;
f(2) = @(t) cos(2*t);
f(3) = @(t) 4*(t^3);
# access properties
size(f)(1); # access the number of functions
f{1} # access the first function
f{2}(17) # evaluate the second function at x = 17
Answer from Gabrielle Stewart on Stack OverflowVideos
What is the Difference Between an Array and a Matrix in MATLAB?
What Are the Two Ways to Initialise an Array?
What Is a Knowledge Pass, and How Does It Work?
Alternatively,
f = cell(3,1); % create a cell array
% initialize
f{1} = @(t) t^2;
f{2} = @(t) cos(2*t);
f{3} = @(t) 4*(t^3);
% access properties
size(f)(1); % access the number of functions
f{1} % access the first function
f{2}(17) % evaluate the second function at x = 17
A cell array is a data type with indexed data containers called cells, where each cell can contain any type of data.
Documentation: https://www.mathworks.com/help/matlab/ref/cell.html
Alternatively, In Octave:
f = cell(3,1); # create a cell array
# initialize
f(1) = @(t) t^2;
f(2) = @(t) cos(2*t);
f(3) = @(t) 4*(t^3);
# access properties
size(f)(1); # access the number of functions
f{1} # access the first function
f{2}(17) # evaluate the second function at x = 17
This should work, just make the function output a vector:
y = @(t,y) [10*(y(2)-y(1)), y(1)*(28-y(3))-y(2), y(1)*y(2)-8*y(3)/3];
y(1,[1;2;3])
size(y(1,[1;2;3]))
exp is a vectorized operation:
B = exp(A);
It doesn't go much more elegant than this ;)
Note that most operations in Matlab are vectorized by default thus you do not need to explicitly loop through all the elements of the matrix.
In the context of good MATLAB practice it's almost always best to attempt to utilize vectorized operations that are built into MATLAB, as Shai's answer speaks to.
However, to answer the explicit question, functions like arrayfun, cellfun, and structfun. These functions can apply a function to each element of an array, cell array, and structure, respectively. This is useful for cases where there is no built-in for what you want to do or it is not a vectorized operation.
For example, with arrayfun:
A = [1, 2; 3, 4];
B = arrayfun(@(x) exp(x), A);
C = exp(A);
test = all(B(:) == C(:)) % Test for equivalence
And test returns true.