How to take the co-ordinate values in MatLab
Mostrar comentarios más antiguos
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 comentarios
Adam Danz
el 13 de Jul. de 2018
Where are the X and Y values within the A matrix? Another way of asking that question, which values of A correspond with X and which with Y?
vigneshwaran Loganathan
el 13 de Jul. de 2018
Image Analyst
el 15 de Jul. de 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.
Respuesta aceptada
Más respuestas (2)
Adam Danz
el 13 de Jul. de 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 comentarios
vigneshwaran Loganathan
el 14 de Jul. de 2018
Adam Danz
el 15 de Jul. de 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.
Image Analyst
el 15 de Jul. de 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 comentarios
Adam Danz
el 15 de Jul. de 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
el 16 de Jul. de 2018
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!