how to return two results by one output when using arrayfun()?

44 views (last 30 days)
f=@(X) fun1(x);
data=arrayfun(f,xgroup);
about funtion 'fun1', now shall I return two result into the 'data' for one element of xgroup;
funtion re=fun1(x)
....
out1=x^2;
out2=x^3;
re=[out1 out2];
end
When I do like above, Matlab cant accept it; so is there some method to return several results by one 'var'?
  7 Comments
Stephen23
Stephen23 on 1 Feb 2016
My aim is to show you some good choices, but because I do not know the rest of your data processing I can't advise on which solution is best for your situation: it depends on how you are generating and handling this data.
Pick the solution that makes the most sense to you, or that you find easiest to code. (Writing code that makes sense saves more time than trying to write slightly faster code that is harder to understand and causes problems...)
For "large" data separate outputs may be faster, but the only way to check is to try both solutions and time them.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 1 Feb 2016
Edited: Stephen23 on 1 Feb 2016
Solution 1: You actually don't need arrayfun at all.
The simplest, neatest and fastest MATLAB code uses code vectorization, very simply like this:
>> x = [1,2,3];
>> y = x.^2
y =
1 4 9
>> z = x.^3
z =
1 8 27
Solution 2: Return TWO Outputs
function [sq,cu] = myFunTwo(x) % two outputs
sq = x^2;
cu = x^3;
end
and call it:
>> [outSqr,outCub] = arrayfun(@myFunTwo,[1,2,3])
outSqr =
1 4 9
outCub =
1 8 27
Solution 3: Use arrayfun's "UniformOutput" Option
function out = myFunOne(x) % one output
sq = x^2;
cu = x^3;
out = [sq,cu];
end
and call it:
>> outCell = arrayfun(@myFunOne,[1,2,3],'UniformOutput',false);
>> outCell{:}
ans =
1 1
ans =
4 8
ans =
9 27
The first solution is by far the simplest and fastest.

More Answers (1)

Guillaume
Guillaume on 1 Feb 2016
It's not clear what sort of output you are looking for out of arrayfun. You have two options:
  • Use a single output function that outputs a vector, as in your example:
function re = fun1(x) %function is designed to work on scalars only
re = [x^2 , x^3]
end
If you use this function in the arrayfun call the output is non-scalar and thus you need to tell arrayfun. As a result, arrayfun will pack the outputs in a cell array:
result = arrayfun(@fun1, 1:5, 'UniformOutput', false) %create a cell array
If you want, you can then convert the cell array into a 2 column matrix:
result = vertcat(result{:})
  • Second option: Use a two outputs function and therefore have two output arrays out of arrayfun:
function [out1, out2] = fun2(x)
out1 = x^2;
out2 = x^3;
end
[re1, re2] = arrayfun(@fun1, 1:5)
You can of course concatenate the output directly into a 2d matrix:
[result(1, :), result(2, :)] = arrayfun(@fun1, 1:5)
Finally, I'll note that in this particular example, arrayfun is completely unnecessary:
result = [x.^2; x.^3] %would do the same
  1 Comment
vx2008
vx2008 on 1 Feb 2016
Thank you for your time spent on this question;
I learn much from your this reply;
Thank you.

Sign in to comment.

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!