Added functionallity for ind2sub

조회 수: 4 (최근 30일)
jwcs
jwcs 2016년 6월 10일
답변: Guillaume 2016년 6월 10일
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

채택된 답변

Walter Roberson
Walter Roberson 2016년 6월 10일
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
jwcs
jwcs 2016년 6월 10일
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 2016년 6월 10일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Guillaume
Guillaume 2016년 6월 10일
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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by