1

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.

3 Answers 3

1
  1. The best way to do that in Matlab is to use an intermediate variable:

    temp = matfunc(3);
    temp(1,3)
    
  2. It's possible to do it directly (without an intermediate variable), but not advised: cumbersome and hardly readable. See here.

  3. 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)
    
Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

0

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)"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.