Effacer les filtres
Effacer les filtres

How to take the co-ordinate values in MatLab

2 vues (au cours des 30 derniers jours)
vigneshwaran Loganathan
vigneshwaran Loganathan le 13 Juil 2018
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 commentaires
Adam Danz
Adam Danz le 13 Juil 2018
Modifié(e) : Adam Danz le 13 Juil 2018
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 le 15 Juil 2018
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.

Connectez-vous pour commenter.

Réponse acceptée

Nanditha Nirmal
Nanditha Nirmal le 13 Juil 2018
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 commentaires
vigneshwaran Loganathan
vigneshwaran Loganathan le 14 Juil 2018
Thank alot, it helped
Adam Danz
Adam Danz le 15 Juil 2018
Modifié(e) : Adam Danz le 15 Juil 2018
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);

Connectez-vous pour commenter.

Plus de réponses (2)

Adam Danz
Adam Danz le 13 Juil 2018
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 commentaires
vigneshwaran Loganathan
vigneshwaran Loganathan le 14 Juil 2018
Thanks for your time, the above answer by Nandita solved my case Your code gives me the index value
Adam Danz
Adam Danz le 15 Juil 2018
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.

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 15 Juil 2018
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 commentaires
Adam Danz
Adam Danz le 15 Juil 2018
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 le 16 Juil 2018
Thanks both, it helped and i will consider the suggestion too

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by