How extract a function from a vector?
显示 更早的评论
Hi all,
I have a question about defining function.
In my codes, I defined a vector in which each coordinates is a function of a variable (called m). The goal of my problem is to plot each coordinates of the vector separately. However when I try to define the coordinates as a function, I receive this error
indexing must appear last in an index expression. OR Unbalanced or unexpected parenthesis or bracket.
I tried the four following expressions
f1=@(m) VECTOR(1,1)(m);
f1=@(m) VECTOR(m)(1,1);
f1=@(m) (VECTOR(1,1))(m);
f1=@(m) (VECTOR(m))(1,1);
Does anyone have an idea?
采纳的回答
更多回答(2 个)
Walter Roberson
2013-2-20
Are you trying to create a vector of functions, and in f1 trying to call the first function with the argument "m" ?
If so then you cannot do that using () to index into the vector. It was possible at one time, but it was ambiguous and was removed. Instead you must use a cell array of function handles.
f1 = @(m) VECTOR{1}(m);
Thibault
2013-2-20
0 个投票
1 个评论
Walter Roberson
2013-2-20
So you are trying to call a function with m as its parameter, and that is going to return a vector, and then you wish to index the resulting vector? If so then there is no convenient syntax for doing that in MATLAB. It can be done using subref(), but nicer is to define an extra function to do the indexing.
First = @(v) v(1);
f1 = @(m) First(VECTOR(m));
where VECTOR is a function or function handle.
类别
在 帮助中心 和 File Exchange 中查找有关 Mathematics 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!