Reputation: 5
I want to create a matrix of functions, however I'd like to dynamically generate it. For example:
myMatrix = zeros(3);
test = @(x) x*y;
for ii = 1:3
myMatrix(ii) = test(ii);
end
something like that to generate: @(y) [y, 2*y, 3*y]
I do not have access to the sym
library.
Upvotes: 0
Views: 141
Reputation: 38032
Depending on your purposes, you can make a single function which generates the matrix you have in your example:
>> f = @(y) bsxfun(@times, 1:3, y(:));
>> f(2:5)
ans =
2 4 6
3 6 9
4 8 12
5 10 15
Upvotes: 1
Reputation: 238081
You can't make a matrix of functions, but you can make cell of function handles, e.g.
cellOfFunctions = {};
for i = 1:3
cellOfFunctions{end + 1} = @(y) y*i;
end
Then you can get each handle as follows (for the first function handle):
fh1 = cellOfFunctions{1};
Then execute it with y = 3
:
result = fh1(3);
Upvotes: 3