Added functionallity for ind2sub

4 views (last 30 days)
jwcs
jwcs on 10 Jun 2016
Answered: Guillaume on 10 Jun 2016
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

Accepted Answer

Walter Roberson
Walter Roberson on 10 Jun 2016
You could create a product suggestion with Mathworks, but I doubt they would implement it. You can define you own small .m that has this functionality, by running ind2col and checking nargout to see whether to combine the outputs or not.
By the way, ind2col is not a MATLAB function, and is also not present in the File Exchange. Did you perhaps mean ind2sub() ?
  2 Comments
jwcs
jwcs on 10 Jun 2016
Thanks for catching that, I didn't notice the ind2col/ind2sub, but I meant ind2sub (edited now).
I could do a separate .m, but I have so many functions already that I don't want to make one for a 'common' function. It was just annoying enough in my code to post a question. I looked for a product suggestion, but I couldn't find one- do you know where?
Walter Roberson
Walter Roberson on 10 Jun 2016
File a technical support case and say you want the functionality. They will tell you they have informed the developers about the suggestion. Some day, the developers may even do something about it.
Every good developer develops a library of often-used functions and uses them instead of re-inventing the wheel.
The general issue you are encountering is that MATLAB does not have any mechanism to allow the user to capture and manipulate multiple outputs in the middle of an expression.

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!