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!
채택된 답변
추가 답변 (2개)
Image Analyst
2013년 5월 26일
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
2013년 5월 27일
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
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
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
2021년 3월 24일
what about if xy(k,1) or xy(k,1) are negative numbers?
Thanks
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
2018년 7월 10일
0 개 추천
thanks for the help
카테고리
도움말 센터 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!