필터 지우기
필터 지우기

matrix indexing and manipulation

조회 수: 2 (최근 30일)
dia-2011
dia-2011 2011년 7월 26일
Hi
(Newbie question)
I have a matrix which is Z=131x47 If I do ZA=Z(1:end); am I right in thinking the elements are rearragned columnwise? Say for example I am interested in the element ZA(1,3000), how do I know what the position of this value is in the original matrix Z?
Thanks

채택된 답변

Yoav Livneh
Yoav Livneh 2011년 7월 26일
You can use ind2sub.
[rowIndex, colIndex] = ind2sub(size(Z),Index);
I think this should work.

추가 답변 (2개)

Image Analyst
Image Analyst 2011년 7월 26일
You could use numel() to tell how many elements are in your Z matrix, then check if 3000 <= numel(Z).

Andreas Goser
Andreas Goser 2011년 7월 26일
First, you are correct that this code would arrange elements columnwise. You may have a look at the RESHAPE command for a more general way of reshaping a matrix. Using REHSHAPE or your code however, does not preserve the size of the orginal matrix. You need to store that to be able to invert the reshape.
You may also be interested to know that matrices in MATLAB can be accessed as vectors
X=rand(10,7);
X(5,4)=1 % Setting to one to spot it better
X(35) % element 4*7+5
Maybe there is a way to write your code without creating an additional vector ZA,
  댓글 수: 2
dia-2011
dia-2011 2011년 7월 26일
Thanks. I understand I can go back to the original matrix Z by using reshape(ZA,131,47);
But how can I find the row and column index of for example element 3000?
Andreas Goser
Andreas Goser 2011년 7월 26일
OK, the answer above WRT IND2SUB seems to meet your needs. My additional point was that you may be able to avoid the "conversion".

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by