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?

geofflittle
  • 437
  • 1
  • 3
  • 14
  • 2
    Why are you making a cell at all? Just call the function directly with the double array arguments. – chappjc Mar 06 '14 at 01: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]... – Gastón Bengolea Mar 06 '14 at 01: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. – geofflittle Mar 06 '14 at 02:14

1 Answers1

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);
Marcin
  • 215,873
  • 14
  • 235
  • 294