1

I have a matrix and a vector in MATLAB defined:

A=rand(3);
x=rand(3,1);

And a function that takes these types of input arguments:

b = MacVecProd(A,x);

However, I'd like to use this function's function handle in order to apply it to my values. I thought that I could use cellfun for this, but:

v = {A,x};
cellfun(@MatVecProd_a, v{:})

Gives the error:

Error using cellfun
Input #2 expected to be a cell array, was double instead.

How do I do this correctly?

3
  • 2
    Why are you making a cell at all? Just call the function directly with the double array arguments. Commented Mar 6, 2014 at 1:28
  • why don't you just do A * [x1, x2, ... , xn], where the x's are column vectors..? it would give what you want: [b1, b2, ..., bn]... Commented Mar 6, 2014 at 1:30
  • In reality, my problem is not as simple as the one I posed. I have a large array of functions, each of which takes a matrix and a column vector as inputs. I'd like to apply each function on my matrix and vector and accumulate the results into a matrix. Commented Mar 6, 2014 at 2:14

1 Answer 1

1

You could define your own, special function to call anonymous functions with given parameters, e.g.:

% define special function to call function handles
myfuncall = @(fh, v) fh(v{:});

% execute MacVecProd using myfuncall
b = myfuncall(@MacVecProd, v)

Based on your comment that you have array of functions and you want to execute them for your input arguments, you could do as follows:

  % cell array of function handles
  myFunctioins = {@MacVecProd, @MacVecProd2, @MacVecProd3};

  % execute each function with v parameters
  % I assume you want to execute them for the same input v
  resultCell = cellfun(@(fh) fh(v{:}), myFunctioins, 'UniformOutput', 0);
Sign up to request clarification or add additional context in comments.

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.