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

76 ビュー (過去 30 日間)
Jorge Peñaloza Giraldo
Jorge Peñaloza Giraldo 2019 年 2 月 13 日
回答済み: Jorge Peñaloza Giraldo 2019 年 2 月 14 日
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

採用された回答

Jorge Peñaloza Giraldo
Jorge Peñaloza Giraldo 2019 年 2 月 14 日
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;

その他の回答 (2 件)

Shunichi Kusano
Shunichi Kusano 2019 年 2 月 13 日
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 件のコメント
Jorge Peñaloza Giraldo
Jorge Peñaloza Giraldo 2019 年 2 月 13 日
編集済み: Jorge Peñaloza Giraldo 2019 年 2 月 13 日
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 2019 年 2 月 14 日
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
Steven Lord 2019 年 2 月 13 日
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 件のコメント
Jorge Peñaloza Giraldo
Jorge Peñaloza Giraldo 2019 年 2 月 13 日
Tha idea is not interpolate the values. Also coordinates x and y should match with u.

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

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by