How can I make an array of handles?
4 次查看(过去 30 天)
显示 更早的评论
Hey,
Let's say I have a vector x=[1 2 3 4 5] (I don't know what's in it and the size its size is something I don't know in advance), And I want to make an array of handles which will, let's say, make the following:
f(i) = @(y) 3*x(i)+5*y;
How can I make it? (I'm using Matlab)
This is just a simple function but it will be the same principle I guess.
Thanks!
0 个评论
回答(3 个)
Star Strider
2017-11-15
Using bsxfun:
f = @(x,y) 3*x + 5*y; % ‘Parent’ Function
ary = @(x,y) bsxfun(f, x(:), y(:)'); % Create Virtual Function Array
x = [1 2 3 4 5]; % ‘x’ — Can Be Anything
y = randi(9, 1, 3); % ‘y’ — Can Be Anything
Result = ary(x, y); % Example Use
0 个评论
the cyclist
2017-11-15
编辑:the cyclist
2017-11-15
x = [3 4 5];
i = 2;
f{i} = @(y) 3*x(i)+5*y;
Notice the curly brackets on the left-hand side of the assignment.
Kaushik Lakshminarasimhan
2017-11-15
You don't need an array of handles to do that. You can use arrayfun like this:
x = 1:5;
y = 5;
z = arrayfun(@(x,y) 3*x + 5*y, x(:), repmat(y,length(x),1));
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!