How to take the co-ordinate values in MatLab

2 次查看(过去 30 天)
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 个评论
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
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
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