简体   繁体   中英

equivalent of “apply” in Octave / Matlab?

I have a function in Octave / Matlab such as this (the real one is much, much more complicated)

function result = foo (x, y, z)
  result = x + y + z;
endfunction

The normal way to invoke the function is as follows:

foo (1, 2, 3);

but I'd like to apply it to arguments packaged in an array like this:

myStuff = [1, 2, 3];
apply (foo, myStuff);

or

foo (myStuff);

I haven't been able to find the syntax needed for such an invocation in the documentation or on Google.

This is possible if you place your arguments in a cell array and use the comma-separated list operator : .

For instance:

c = {x, y, z};
foo(c{:});

is equivalent to:

foo(x,y,z);

Best,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM