How to handle unknown output arguments in arrayfun?

1 回表示 (過去 30 日間)
Mr M.
Mr M. 2017 年 5 月 20 日
編集済み: Matt J 2017 年 5 月 21 日
Arrayfun can handle to a function that accepts n input arguments and returns m output arguments. But what if we dont know m? If m depends on the input? For example length of the output randperm(n) is n. How to make a matrix nxk in which each column is a different permutation of 1..n?

採用された回答

Matt J
Matt J 2017 年 5 月 20 日
編集済み: Matt J 2017 年 5 月 21 日
You always know "m". It is determined by the number of output arguments you call the function with.
For example length of the output randperm(n) is n. How to make a matrix nxk in which each column is a different permutation of 1..n?
This is a different question. You would do
n=4;k=3;
outCell=arrayfun(@(p) randperm(n).', 1:k,'uni',0);
matrix=[outCell{:}],
but note that for each p=1,...k, the function randperm is being called with just a single output argument. In other words, m is known and is equal to 1.

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by