how to create a matrix of values from a X and Y coordinates
Show older comments
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.

Accepted Answer
More Answers (2)
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
on 13 Feb 2019
Edited: Jorge Peñaloza Giraldo
on 13 Feb 2019
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.
Steven Lord
on 13 Feb 2019
0 votes
Make a regular grid of Xq and Yq coordinates with meshgrid then pass your scattered data and the grid coordinates to the griddata function.
1 Comment
Jorge Peñaloza Giraldo
on 13 Feb 2019
Categories
Find more on Interpolation 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!