array of function handle
19 次查看(过去 30 天)
显示 更早的评论
I have to convert a code written using syms, to a code with function handle. (my prof doesn't want me to use syms)
how can I select the element of the array?
I write a simpler code
A=@(x)[sin(x), cos(x);1, -1];
B=@(x)[sin(x);cos(x)];
C=@(x) A(x)*B(x); % this is an array of 2 element
D=@(x) C(first element) - C(second element)
0 个评论
采纳的回答
Walter Roberson
2020-6-12
First = @(x) x(1);
Second = @(x) x(2);
D = @(x) First(C(x)) - Second(C(x));
However, this will execute C twice. More efficient would be
FirstMinusSecond = @(x) x(1) - x(2);
D = @(x) FirstMinusSecond(C(x))
2 个评论
Walter Roberson
2020-6-13
Your A and B and C are only valid if x is a scalar, and your D would be unlikely to return the value you want if x is not a scalar. If you want to plot then instead of
plot(l,D(l))
do
y = arrayfun(D, l);
plot(l, y)
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!