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.

Antworten (1)

Andrei Bobrov
Andrei Bobrov am 25 Okt. 2015
Bearbeitet: Andrei Bobrov am 25 Okt. 2015

0 Stimmen

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 Kommentare

jack
jack am 25 Okt. 2015
ok but how can i create a matrix equivalent to that i am trying to reach?
Stephen23
Stephen23 am 25 Okt. 2015
Bearbeitet: Stephen23 am 25 Okt. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

am 25 Okt. 2015

Bearbeitet:

am 25 Okt. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by