Using predefined multiple output matlab function in anonymous function definition: Not getting multiple outputs.
8 ビュー (過去 30 日間)
古いコメントを表示
I am trying to feed an array of linear indices into "ind2sub" using "arrayfun" but without explicitly stating that I need two outputs from ind2sub, it never gives two outputs.
twoDlocation=arrayfun(@(x) ind2sub(sizeArr,x), listofX);
Can anyone tell how to make this happen?
in short x=ind2sub(sizeArr,indx); returns 'x' as a single element. it's only when one writes [x,y]=ind2sub(sizeArr,indx) that one gets both the indices.
0 件のコメント
回答 (2 件)
Adam
2016 年 11 月 7 日
編集済み: Adam
2016 年 11 月 7 日
Have you tried calling as:
[locationX, locationY]=arrayfun(@(x) ind2sub(sizeArr,x), listofX);
?
Anonymous functions do not explicitly state the number of output arguments, but they work like other functions in that if you call them with multiple output arguments then they will return more than one.
e.g.
f = @(x) ind2sub( [4 4], x );
K>> f(9)
ans =
9
K>> [x,y] = f(9)
x =
1
y =
3
This is still the case when an arrayfun is wrapped around the call.
0 件のコメント
Walter Roberson
2016 年 11 月 7 日
[twoDlocation{1}, twoDlocation{2}] = arrayfun(@(x) ind2sub(sizeArr,x), listofX);
and then you can paste together the two cells. Or you could use distinct variable names and past them together.
It is not possible in MATLAB to say "I know that this routine returns 2 distinct outputs, but put them together into a cell array for me!"
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!