Matlab - starting a function with parameters for each element of an array?

Matlab has the good property that scalar functions (such as sin) can work on arrays, work with any element of the array, and return the array as a result.

I have a scalar function f(x,p), where xis the scalar, and pis the parameter (actually an array of parameters). Given a fixed parameter p, I want to run f(x,p)in an array A. In Ruby, it will look like this:

A.collect{|x| f(x,p)}

But I have no idea how to do this in Matlab for functions that take parameters, and not just the scalar from the array in which I want to work.

+5
source share
1 answer

MATLAB , , arrayfun.

arrayfun( @(x) f(x, p), A )

,

A = 1:10;
p = 2;
arrayfun( @(x) x.^p, A )

, , p.

+7

All Articles