Using predefined multiple output matlab function in anonymous function definition: Not getting multiple outputs.
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (2)
Adam
il 7 Nov 2016
Modificato: Adam
il 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 Commenti
Walter Roberson
il 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 Commenti
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!