Added functionallity for ind2sub
Ältere Kommentare anzeigen
I was using ind2sub with a 2D matrix (IND) for a matrix of 2D size and only supplied one output (ans).
Example:
>> ind2sub([3,3], [2,2,2; 3,3,3]);
The function is supposed to have an output of something like
[A,B] = ind2sub(...
however I wanted to use it in the middle of a line, where there is only one output matrix. As it is now, the output of the above example function is [2,2,2; 3,3,3].
My question is if ind2sub could have the added functionality so that if there is only one output matrix, the output matrices could be combined into the first non-utilized dimension. In the example (and in my case) the third dimension (Ex. size(ind2sub(...)) = [2,3,2]).
Have a good day,
Jude
Akzeptierte Antwort
Weitere Antworten (1)
Guillaume
am 10 Jun. 2016
As Walter says, to get that implemented you need to suggest that to Mathworks with a service request. However, I'm very doubtful they'd consider it.
You're basically asking to concatenate the normal outputs [out1, out2, out3, ...] into a single matrix when you're only requesting one output. The question is then how does ind2sub differentiate between matrix output, and you not caring about the other out? If I just want the rows of a 2D matrix, I still have to ask the columns otherwise I get your matrix output. That's not the way matlab normally works. More importantly, it stands a good chance of breaking existing code.
I guess you could add a flag to request that behaviour, but more importantly I don't see the point. What goes is it to get the various dimension indices altogether in the same matrix. You can't use them for indexing like that anyway.
Also, the dimension where the various subscript appear vary depending on the number dimension of the index input. It's always ndims(indices) + 1 (If you pass a 3D matrix of indices, the subscripts are in the 4th dimension of the output). Something you would have to watch out for in your calling code.
"I could do a separate .m, but I have so many functions". So? Matlab also comes with a huge number of functions. You can organise functions in folders or even packages. Your function is trivial to write as well:
function m = myind2sub(sz, indices)
m = cell(size(sz));
[m{:}] = ind2sub(sz, indices);
m = cat(ndims(indices)+1, m{:});
end
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!