matrix to matrix of indices
Show older comments
hello guys,
i need your help in solving the following problem:
given a matrix of n dimensions, how can i convert it to matrix of indices as seen bellow:

Thanks in advance.
Answers (1)
Andrei Bobrov
on 25 Oct 2015
Edited: Andrei Bobrov
on 25 Oct 2015
A = rand(3);
[ii,jj] = ind2sub(size(A),1:numel(A));
EDIT
[ii,jj] = ndgrid(1:3);
out = arrayfun(@(x,y)sprintf('%d,%d',x,y),ii,jj,'un',0);
or
n = size(A);
[ii,jj] = ind2sub(n,1:numel(A));
out = reshape(arrayfun(@(x,y)sprintf('%d,%d',x,y),ii,jj,'un',0),n);
2 Comments
jack
on 25 Oct 2015
Two elements (i.e. two values) do not fit into one position, so what you showed us in your question cannot be made into a 3x3 matrix. Andrei Bobrov showed you how you can get the X and Y matrices separately.
If you want the X and Y values together in one array then you could:
- use a 3x3x2 array
- use some encoding to combine the elements
- use a character array and some encoding
but really the simplest solution is exactly as Andrei Bobrov has given you already.
If you tell us what you actually want to do with this matrix, then we can help you figure out the best way to store those indices.
Categories
Find more on Matrix Indexing 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!