Tim
Tim

Reputation: 99428

An anonymous function can have more than one output arguments?

From Mathworks

An anonymous function consists of a single MATLAB expression and any number of input and output arguments.

I wonder how an anonymous function can have more than one output arguments? Thanks and regards!

Upvotes: 3

Views: 373

Answers (2)

bastibe
bastibe

Reputation: 17189

You can easily return multiple values from an anonymous function using deal:

meanAndStd = @(x)deal(mean(x), std(x));
[meanValue, stdValue] = meanAndStd(randn(1000));

Upvotes: 0

goric
goric

Reputation: 11885

When the expression which your anonymous function is executing can return more than one value, then so can your anonymous function. For example, using the max function which can return both the max value of an array and its index:

arr = [1 2 4 3];
anon = @(y) max(y);
[maxVal, ind] = anon(arr); 

Upvotes: 7

Related Questions