필터 지우기
필터 지우기

How to take the co-ordinate values in MatLab

조회 수: 2 (최근 30일)
vigneshwaran Loganathan
vigneshwaran Loganathan 2018년 7월 13일
댓글: vigneshwaran Loganathan 2018년 7월 16일
A = [1 0 3 3;
0 0 4 5;
5 0 98 56;
0 2 4 6]
In the above 4*4 matrix, each cell has got x and y coordinates. I need to extract the x and y co-ordinate values for the cells excepting 0.
For example, the coordinates are placed in the below manner having the zero cell omitted
x 1 1 1,....
y 1 3 4,....
Any leads?
  댓글 수: 4
Adam Danz
Adam Danz 2018년 7월 13일
편집: Adam Danz 2018년 7월 13일
I see, so the values in 'A' are all y values and the row indices are the x values. I'll post an answer in a few minutes.
Image Analyst
Image Analyst 2018년 7월 15일
Vigneshwaran, what a bad way to name variables! Why deliberately choose variable names that fly in the face of hundreds of years of convention? You're choosing x to be the vertical/row coordinate instead of the conventional y. And you're choosing the horizontal/column coordinate of the matrix to be y, instead of the conventional x. Why? Why do it the opposite of everyone else in the world for no reason???
By the way, if you had given the full x and y instead of just the first three numbers and ..., you could have prevented a lot of confusion.

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

채택된 답변

Nanditha Nirmal
Nanditha Nirmal 2018년 7월 13일
Hi,
The code below will give you the coordinates you want. This is what you would want to do:
A = [1 0 3 3;0 0 4 5;5 0 98 56; 0 2 4 6];
x=[];
y=[];
for i=1:4
for j=1:4
if A(i,j) ~= 0
x=[x ;i];
y=[y ;j];
end
end
end
  댓글 수: 2
vigneshwaran Loganathan
vigneshwaran Loganathan 2018년 7월 14일
Thank alot, it helped
Adam Danz
Adam Danz 2018년 7월 15일
편집: Adam Danz 2018년 7월 15일
You can avoid both for-loops and speed up the code with
%Your data (y values)
A = [1 0 3 3;0 0 4 5;5 0 98 56; 0 2 4 6];
% Transpose A (to match your example)
aT = A';
% Your x values
x = repmat((1:size(aT,1))', 1, size(aT,2));
y = repmat(1:size(aT,1), size(aT,2), 1);
% Identify where A==0
zeroIdx = aT==0;
% Extract coordinates where A~=0
xCoord = x(~zeroIdx);
yCoord = y(~zeroIdx);

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

추가 답변 (2개)

Adam Danz
Adam Danz 2018년 7월 13일
If I'm understanding correctly, your matrix A are all y values and the row indices are the X values (based on your comments above).
%Your data (y values)
A = [1 0 3 3;0 0 4 5;5 0 98 56; 0 2 4 6];
% Your x values
x = repmat((1:size(A,1))', 1, size(A,2));
% Identify where A==0
zeroIdx = A==0;
% Extract coordinates where A~=0
xCoord = x(~zeroIdx);
yCoord = A(~zeroIdx);
% Test: draw your data, circle the chosen coordinates
figure;
plot(A);
hold on
plot(xCoord, yCoord, 'ko')
  댓글 수: 2
vigneshwaran Loganathan
vigneshwaran Loganathan 2018년 7월 14일
Thanks for your time, the above answer by Nandita solved my case Your code gives me the index value
Adam Danz
Adam Danz 2018년 7월 15일
Nanditha's code gives you the index values. The for-loops in her code record the values of i and j which are index values of A. My code provides the index values for x and the 'A' values for y which is what I thought you were describing by, " if you plot the matrix, the output will have x,y and the index values".
See the comment I left under Nanditha's solution for a simplification.

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


Image Analyst
Image Analyst 2018년 7월 15일
Personally I'd use meshgrid() - a useful function that you might want to learn about:
[rows, columns] = size(A)
[x, y] = meshgrid(1:columns, 1:rows)
mask = A~=0
x = x(mask)
y = y(mask)
  댓글 수: 2
Adam Danz
Adam Danz 2018년 7월 15일
Vigneshwaran, this is the fastest, simplest solution. However, if you want it to conform to your example, you'll need to transpose A and switch the values of x and y.
aT = A';
[rows, columns] = size(aT)
[y, x] = meshgrid(1:columns, 1:rows)
mask = aT~=0
x = x(mask)
y = y(mask)
But reconsider the names of your x and y variables as suggested by Image Analyst above.
vigneshwaran Loganathan
vigneshwaran Loganathan 2018년 7월 16일
Thanks both, it helped and i will consider the suggestion too

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

Community Treasure Hunt

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

Start Hunting!

Translated by