フィルターのクリア

How to take the co-ordinate values in MatLab

2 ビュー (過去 30 日間)
vigneshwaran Loganathan
vigneshwaran Loganathan 2018 年 7 月 13 日
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