Convert xy Coordinates to Matrix

43 Ansichten (letzte 30 Tage)
John
John am 26 Mai 2013
Kommentiert: Image Analyst am 24 Mär. 2021
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!

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 27 Mai 2013
Bearbeitet: Andrei Bobrov am 27 Mai 2013
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 Kommentare
John
John am 27 Mai 2013
Now I get it, thanks for the clarification.
cecilia dip
cecilia dip am 28 Nov. 2016
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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Image Analyst
Image Analyst am 26 Mai 2013
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 Kommentare
Luigi Izzo
Luigi Izzo am 24 Mär. 2021
what about if xy(k,1) or xy(k,1) are negative numbers?
Thanks
Image Analyst
Image Analyst am 24 Mär. 2021
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.

Melden Sie sich an, um zu kommentieren.


Annisa Dewanti Putri
Annisa Dewanti Putri am 10 Jul. 2018
thanks for the help

Kategorien

Mehr zu Elementary Polygons finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by