Combining and sorting two meshgrids

25 vues (au cours des 30 derniers jours)
Lucas Warwaruk
Lucas Warwaruk le 3 Nov 2020
Say I have four meshgrids with values that are offset by a constant value along, x, y and both x and y:
% Grid No. 1
x1 = 1:1:10;
y1 = 1:1:10;
[X1,Y1] = meshgrid(x1,y1);
offset = 0.5;
% Grid No. 2
x2 = x1 + offset;
y2 = y1;
[X2,Y2] = meshgrid(x2,y2);
% Grid No. 3
x3 = x1;
y3 = y1 + offset;
[X3,Y3] = meshgrid(x3,y3);
% Grid No. 4
x4 = x1 + offset;
y4 = y1 + offset;
[X4,Y4] = meshgrid(x4,y4);
Corresponding to these four different grids are discrete data-sets that represent say velocity, for example:
v1 = rand(10,10); % Velocity corresponding to x1,y1
v2 = rand(10,10); % Velocity corresponding to x2,y2
v3 = rand(10,10); % Velocity corresponding to x3,y3
v4 = rand(10,10); % Velocity corresponding to x4,y4
How can you combine and sort the four velocity matrces into a single matrix based on a combination of the four x,y grids? I believe in the elementary example I listed above, the results would be a velocity matrix that is 20x20. The velocity values would correspond to position:
x = 1:offset:10;
y = 1:offset:10;
[X,Y] = meshgrid(x,y);

Réponse acceptée

Akira Agata
Akira Agata le 4 Nov 2020
How about the following?
Xall = [X1(:); X2(:); X3(:); X4(:)];
Yall = [Y1(:); Y2(:); Y3(:); Y4(:)];
vall = [v1(:); v2(:); v3(:); v4(:)];
[~, loc] = ismember([X(:), Y(:)], [Xall, Yall], 'rows');
Z = vall(loc);
Z = reshape(Z,size(X));
  1 commentaire
Lucas Warwaruk
Lucas Warwaruk le 4 Nov 2020
It works. Thank you sir!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Vector Fields dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by