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?

 Accepted Answer

Thibault
Thibault on 20 Feb 2013
Works fine.
Thanks for your time.

1 Comment

Please accept the answer which solves your problem. And post comments in the comment section, not as an answer. I suggest to delete this answer and accept the one, which helped. Thanks.

Sign in to comment.

More Answers (2)

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
Thibault on 20 Feb 2013
The problem is that I defined the vector using multiplication of matrices. So basically, the final vector is defined by inverting a matrix that depends on m.
And because this vector stands for the displacement of several elements, I need to plot each coordinates one by one

1 Comment

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.

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Products

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!