how to create a matrix of values from a X and Y coordinates

76 views (last 30 days)
Hi
I have a vector u (dots) with its x,y coordinates. Each vector has the same size. I wanto to create a matrix u using x,y coordinates. How I can do it, taking into account that x and y don't form a perfect rectangle.? I mean, there are empty values of u without their coordiantes x,y respectively as you can see in the figure. Empty values can be filled by NaN.
dots.jpg

Accepted Answer

Jorge Peñaloza Giraldo
Jorge Peñaloza Giraldo on 14 Feb 2019
Thank you everyone for you help.
I all ready solve using the following code.
d = [2.0000 0 0.3800;
2.0000 5.0000 0.3480;
2.0000 25.0000 0.3280;
3.0000 2.0000 0.3190;
3.0000 50.0000 0.2120;
5.0000 10.0000 0.1380;
5.0000 60.0000 0.1100;
5.0000 100.0000 0.0870;
7.0000 5.0000 0.0690;
7.0000 9.0000 0.4113;
7.0000 45.0000 0.3807;
7.0000 95.0000 0.3678;
9.0000 10.0000 0.3207;
9.0000 35.0000 0.1765;
15.0000 65.0000 0.1123;
15.0000 75.0000 0.0917;
15.0000 99.0000 0.0727;
15.0000 125.0000 0.0573;
15.0000 150.0000 0.3811];
X = d(:,1); Y = d(:,2); Z = d(:,3);
Xs = unique(X);
Ys = unique(Y);
Xi = arrayfun( @(x) find(Xs==x), X );
Yi = arrayfun( @(y) find(Ys==y), Y );
Li = Yi + (Xi-1) * numel(Ys);
XYZ = nan(numel(Ys), numel(Xs));
XYZ( Li ) = Z;

More Answers (2)

Shunichi Kusano
Shunichi Kusano on 13 Feb 2019
First, the xy cordinate must be converted to indices which are the coordinates of your matrix.
% dx, dy: minimum sampling interval of your x, y coordinates.
xi = x / dx;
xi = xi - min(xi) + 1; % shift so that the minimum index is 1.
yi = y / dy;
yi = yi - min(yi) + 1;
Then, matrix size is decided from max(xi) and max(yi).
u_mat = NaN(max(yi), max(xi));
Finally, the value u is stored in u_mat. Before that, the indices of xi and yi is converted into linear index.
lin_i = sub2ind(size(u_mat), yi, xi);
u_mat(lin_i) = u;
Some modification may be needed. hope this helps.
  2 Comments
Jorge Peñaloza Giraldo
Jorge Peñaloza Giraldo on 13 Feb 2019
Edited: Jorge Peñaloza Giraldo on 13 Feb 2019
Thank you for your answer.
I have a problem with dy beacuse the minimum is cero. There are many coordinates that are repeated. Do you have another idea? Also my dx and dy are not iinteger numbers. Therefore, creating a matrix using NaN does not work.
Shunichi Kusano
Shunichi Kusano on 14 Feb 2019
for the problem on dy, please remove the repetetion by unique command. Then, dy will be correctly computed:
dy = min(diff(unique(y)))
But, if your xi and yi include non-integer (or your data cannnot be on a uniform grid), the proposed procedure does not work as you mentioned.
By the way, it seems that you can avoid interpolation with griddata. Just replace interpolated values to NaN at the point where you don't have u.
I'm sorry, I have no more idea so far.

Sign in to comment.


Steven Lord
Steven Lord on 13 Feb 2019
Make a regular grid of Xq and Yq coordinates with meshgrid then pass your scattered data and the grid coordinates to the griddata function.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!