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.

답변 (1개)

Andrei Bobrov
Andrei Bobrov 2015년 10월 25일
편집: Andrei Bobrov 2015년 10월 25일

0 개 추천

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

jack
jack 2015년 10월 25일
ok but how can i create a matrix equivalent to that i am trying to reach?
Stephen23
Stephen23 2015년 10월 25일
편집: Stephen23 2015년 10월 25일
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.

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

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

질문:

2015년 10월 25일

편집:

2015년 10월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by