Convert xy Coordinates to Matrix

I have an xy coordinates positions (100x2) and another z vector (100x1) with values corresponding to each xy coordinate. How can I make a matrix of the coordinates with the position of each coordinate having the corresponding z value? Thanks!

 採用された回答

Andrei Bobrov
Andrei Bobrov 2013 年 5 月 27 日
編集済み: Andrei Bobrov 2013 年 5 月 27 日

2 投票

after John's comment in Image Analyst's answer:
out = accumarray([x(:),y(:)],z(:),[10 10]);
or
out = zeros(10);
out(sub2ind(size(out),x,y)) = z;

4 件のコメント

John
John 2013 年 5 月 27 日
Thanks! it works now :)
Andrei Bobrov
Andrei Bobrov 2013 年 5 月 27 日
編集済み: Andrei Bobrov 2013 年 5 月 27 日
Hi John! NOTE: Second part my answer (code after 'or') is the same as the Image Analyst' answer...
John
John 2013 年 5 月 27 日
Now I get it, thanks for the clarification.
cecilia dip
cecilia dip 2016 年 11 月 28 日
Hi, I have to do the same thing, and i've tried this, but my coordinates (x,y) are negative and non-integer numbers, as they are latitude,longitude.. how can i do this? I want a plot where for each(lat,long) i can have my Z value (in a color scale, as i will compare it with interpolation methos later). Thank you!

サインインしてコメントする。

その他の回答 (2 件)

Image Analyst
Image Analyst 2013 年 5 月 26 日

1 投票

Try this:
% Setup / initialization.
% Start out matrix as zeros.
m = zeros(20,10);
% Generate 100 random coordinates.
xy = int32(randi(10, 100, 2));
% Get matrix values for those x,y locations
z = randi(255, 100, 1); % 100 values.
% Now, do what the poster, John, wants to do.
% Assign the z values to the (x,y) coordinates at the corresponding row.
% E.g. m at (x(1), y(1)) will have a value of z(1).
% m at (x(2), y(2)) will have a value of z(2). And so on.
indexes = sub2ind([20, 10], xy(:,1), xy(:,2))
m(indexes) = z

6 件のコメント

John
John 2013 年 5 月 27 日
Thanks for your help, unfortunately it didn't work. Here's my data. x and y are the coordinates and z is the corresponding value for each coordinate.
x [4 7 5 9 3 5 5 2 1 1 ]
y [9 2 9 7 9 9 1 4 7 6 ]
z [1 0 1 1 1 0 1 0 1 0 ]
I wish to create a 10x10 matrix (since my xy coordinates are between 1 and 10). In this matrix, I want each coordinate, say for instance, the first xy coordinate (4,9) whose z value is 1 is shown as 1 in column 4 row 9 of the matrix.
Image Analyst
Image Analyst 2013 年 5 月 27 日
Of course it DOES work if you adapt it to a 10 by 10.
m=zeros(10);
indexes = sub2ind([10, 10], x, y)
m(indexes) = z
When I did my example, I picked random numbers for x, y, and z, and random sizes. You were supposed to know that and be able to make the simple adaptations yourself. But anyway, andrei did it for you so you're all set now.
MP
MP 2018 年 6 月 14 日
Hello, what if I have a huge matrix 2000 x 2000 full of zeros and an XY matrix with 2300 x,y values. How can I insert the value 1 in my zero matrix (2000 X 2000) where I have an x,y value (from my XY matrix)?
Image Analyst
Image Analyst 2018 年 6 月 14 日
That's not huge, far from it. You can do intuitive, quick and simple for loop
for k = 1 : length(xy)
row = xy(k, 2); % y is row
col = xy(k, 1); % x is column
m(row, col) = 1;
end
Luigi Izzo
Luigi Izzo 2021 年 3 月 24 日
what about if xy(k,1) or xy(k,1) are negative numbers?
Thanks
Image Analyst
Image Analyst 2021 年 3 月 24 日
Try using a scatteredInterpolant. Demo attached.
If you still need help, attach your x, y, and z data in a new question (not here) so people can help you.

サインインしてコメントする。

Annisa Dewanti Putri
Annisa Dewanti Putri 2018 年 7 月 10 日

0 投票

thanks for the help

カテゴリ

質問済み:

2013 年 5 月 26 日

コメント済み:

2021 年 3 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by