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.

回答(2 个)

Adam
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.

Walter Roberson
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!"

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by