Multiple Outputs of a function into a single vector

98 ビュー (過去 30 日間)
Joseph
Joseph 2022 年 9 月 20 日
コメント済み: Joseph 2022 年 9 月 20 日
I am a new Matlab programmer, and am familiar with functional languages such as python. In my university Matlab course, we are required to write test functions similar to this one:
function [x,y] = f(a, b)
x = a;
y = b;
end
This function is called with a number of different parameters, and I'd like to store them in one large array. Previous answers I've found lead me to this solution:
returns = zeros(4, 2);
[returns(0,0), returns(0,1)] = f(a0, b0);
[returns(1,0), returns(1,1)] = f(a1, b1);
[returns(2,0), returns(2,1)] = f(a2, b2);
[returns(3,0), returns(3,1)] = f(a3, b3);
but when "returns" has more than two outputs, this gets ugly very fast. The only other solution I've found instead breaks this into two lines:
returns = zeros(4, 2);
[A, B] = f(a0, b0);
returns(0, :) = [A, B];
[A, B] = f(a1, b1);
returns(1, :) = [A, B];
[A, B] = f(a2, b2);
returns(2, :) = [A, B];
[A, B] = f(a3, b3);
returns(3, :) = [A, B];
But if I merge those two lines, it broadcasts A into every element of the vector. In Python, I could re-cast the output as a numpy array with almost identical syntax. Is there a similar function in Matlab? Every output has the same data type. I'm aware this can be solved with for loops, but I'm interested if a functional solution exists.

採用された回答

Walter Roberson
Walter Roberson 2022 年 9 月 20 日
There is no functional solution in MATLAB. The only way to capture multiple outputs is an assignement statement. The assignment can be to an expansion such as
[returns{1:5}] = f(a3, b3)
but you cannot gather the outputs "in-line" like
min({f{a3,b3)}) %will not work to capture multiple outputs
  1 件のコメント
Joseph
Joseph 2022 年 9 月 20 日
This works! Its unfortunate I can't use full functional syntax, but this solves my immediate problem in a very clean way.
returns = cell(4, 2);
[returns{1, 1:2}] = f(a1, b1)
[returns{2, 1:2}] = f(a2, b2)
[returns{3, 1:2}] = f(a3, b3)
[returns{4, 1:2}] = f(a4, b4)
returns = cell2mat(returns)
I want to highlight James's solution for anyone else stumbling on this answer. This one works for my workflow, but his is also a very good answer. I wish I could accept both.

サインインしてコメントする。

その他の回答 (1 件)

James Tursa
James Tursa 2022 年 9 月 20 日
You could modify f( ) to return a vector. Or if you didn't want to modify f( ) you could create a helper function that does this. E.g.,
function v = f2(a,b)
[A,B] = f(a,b);
v = [A,B];
end
Then just call f2( ) instead of f( ).
Another approach is to have the helper function use the nargout/varargout feature to detect how many outputs are requested by the caller. E.g.,
function varargout = f2(a,b)
[A,B] = f(a,b);
if nargout == 2
varargout{1} = A;
varargout{2} = B;
else
varargout{1} = [A,B];
end
end
  1 件のコメント
Joseph
Joseph 2022 年 9 月 20 日
This is extremely handy. I will definitely be using this in the future. I wish I could accept both answers, because they're both very situational.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by