MATLAB Answers

Combining and sorting two meshgrids

조회 수: 34(최근 30일)
Lucas Warwaruk
Lucas Warwaruk 2020년 11월 3일
댓글: Lucas Warwaruk 2020년 11월 4일
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);

채택된 답변

Akira Agata
Akira Agata 2020년 11월 4일
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
Lucas Warwaruk
Lucas Warwaruk 2020년 11월 4일
It works. Thank you sir!

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by