Using predefined multiple output matlab function in anonymous function definition: Not getting multiple outputs.
4 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Adam
on 7 Nov 2016
Edited: Adam
on 7 Nov 2016
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 Comments
Walter Roberson
on 7 Nov 2016
[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 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!