Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I capture multiple return values in a call to MATLAB's arrayfun?

I have a function that takes an image as an argument and produces a label and a score as a result. I occasionally want to quickly test over a (cell) array of images, and the most convenient way I know to do this is using arrayfun. This works fine for getting the labels my function produces, but I would really like the output to be a list of [label score] cells.

I could write a wrapper around my function that captures both values and returns them as a cell matrix, and then call that wrapper within arrayfun, but it seems like this is a common enough idiom that there should be a way to work with multiple return values more conveniently. Is there? (Perhaps a standard convenience function already exists that can do this? Sort of like the opposite of deal...)

like image 812
Kaelin Colclasure Avatar asked May 28 '12 19:05

Kaelin Colclasure


1 Answers

You can get your output as two matrices by using built-in syntax:

  [A, B, ...] = arrayfun(fun, S, ...);

For example:

function [y,z]=foo(x)
     y= x*x;
     z = x + 10;
end

And then run the function :

[A,B] = arrayfun( @foo, magic(5))
like image 53
Andrey Rubshtein Avatar answered Oct 29 '22 10:10

Andrey Rubshtein