Added functionallity for ind2sub
4 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 个评论
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
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!