Using predefined multiple output matlab function in anonymous function definition: Not getting multiple outputs.
6 次查看(过去 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!