필터 지우기
필터 지우기

Converting a matrix of points to a list of x & y coordinates

조회 수: 21 (최근 30일)
Douglas
Douglas 2014년 8월 18일
댓글: Douglas 2014년 8월 18일
Hello,
I have a matrix of size n*n, filled with zeros. Some points are filled with A's. I'd like to take this matrix and turn it into two arrays, one with the x-coordinates of each A and the other with the y-coordinates of each A.
I have seen numerous ways of going the other way, i.e. from a list of coordinates to a matrix, but none going this way.
Any help is greatly appreciated!
  댓글 수: 1
Douglas
Douglas 2014년 8월 18일
The reason I am looking to do this is to be able to use the scatter(X,Y) function to plot a sort of map of the matrix I have.
Is there perhaps a better way to do this?

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

채택된 답변

Guillaume
Guillaume 2014년 8월 18일
Use the two arguments output of find (rather than find + ind2sub):
[y, x] = find(matrix);
  댓글 수: 1
Douglas
Douglas 2014년 8월 18일
I went with this, seems the most straight-forward. Thank you!

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

추가 답변 (2개)

Iain
Iain 2014년 8월 18일
Index_of_As = find( matrix );
[down along] = ind2sub(size(matrix),Index_of_As);

Adam
Adam 2014년 8월 18일
편집: Adam 2014년 8월 18일
ind1d = find( myMatrix == 'A' );
[y, x] = ind2sub( [n n], ind1d );
should be close to doing the job, although it depends what you mean by 'A'. Do you have a cell array? The above code would work if, instead of 'A', you had some numerical value, but otherwise it would likely need a bit of syntactical modification depending if you have a cell array.

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by