Reputation: 37
let's Consider a (vector or Matrix) function Like below:
matfunc=@(x)[x^2 cos(x) e^x 3*x];
I want to access to an specified array,Like (1,3) at x=3. How Can I Do this in MATLAB? I've tried:
matfunc(3)(1,3)
(matfunc(3))(1,3)
matfunc(3,1,3)
but it doesn't Work.
Upvotes: 1
Views: 60
Reputation: 261
The best is to write [a]=matfunc(input); a(1,3) % this gives you the element 1,3 of "a" which is output of "matfunc(input)"
Upvotes: 0
Reputation: 18484
You're trying to use incorrect syntax. You need to evaluate the function first and then you can index into the resultant variable:
A = matfunc(3);
A(1,2)
You probably won't like this if you're trying to get everything on one line, but that's how Matlab works. If you really want to put this on one line, you can define a helper function (on another line) that performs the indexing:
index = @(A,i,j)A(i,j);
index(matfunc(3),1,2)
Upvotes: 1
Reputation: 112659
The best way to do that in Matlab is to use an intermediate variable:
temp = matfunc(3);
temp(1,3)
It's possible to do it directly (without an intermediate variable), but not advised: cumbersome and hardly readable. See here.
Another possibility is to use a cell array of functions (instead of a vector function):
matfunc = {@(x) x^2, @(x) cos(x), @(x) exp(x), @(x) 3*x};
With this approach you can combine the two indexings (first the cell index to select function component; then the input argument):
matfunc{3}(3)
Upvotes: 1