How do I get all the outputs of the ind2sub function ?

2 views (last 30 days)
Say I have a variable dimension matrix A (it could have 2,3 ..n dimensions) so I could access it by A(x1,x2,...,xn). When I use a linear index 'lin_ind' how can get the matrix coordinates for that point ?
I could use
ind2sub(size(A),lin_ind)
but the problem is how to get 'all' the outputs for a variable dimension?

Accepted Answer

Stephen23
Stephen23 on 27 Jan 2016
Edited: Stephen23 on 27 Jan 2016
You can preallocate a cell array to the same size as the siz input, and use this to collect all of the outputs. This will expand to whatever size siz has:
>> idx = [3 4;5 6];
>> siz = [2,2,2];
>> out = cell(size(siz));
>> [out{:}] = ind2sub(siz,idx)
out =
[2x2 double] [2x2 double] [2x2 double]

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!