How extract a function from a vector?
Mostra commenti meno recenti
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?
Risposta accettata
Più risposte (2)
Walter Roberson
il 20 Feb 2013
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
il 20 Feb 2013
0 voti
1 Commento
Walter Roberson
il 20 Feb 2013
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.
Categorie
Scopri di più su Matrix Indexing in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!