How to overlap two grids (matrices) created with meshgrid

6 views (last 30 days)
I've created two separate matrices using meshgrid:
clear all; clc;
dx = 2;
dy = 2;
bdyx_A = 0:dx:50;
bdyy_A = 0:dy:50;
bdyx_B = 20:dx:30;
bdyy_B = 20:dx:30;
[X_A,Y_A] = meshgrid(bdyx_A,bdyy_A);
[X_B,Y_B] = meshgrid(bdyx_B,bdyy_B);
% Calculate some parameter as a function of your x and y
Z_A = sqrt(X_A.^2 + Y_A.^2);
Z_B = 3.*sqrt(X_B.^2 + Y_B.^2);
% Next, reshape all of our matrices into vectors:
G_A = [reshape(X_A,size(X_A,1)*size(X_A,2),1)...
reshape(Y_A,size(Y_A,1)*size(Y_A,2),1)...
reshape(Z_A,size(Z_A,1)*size(Z_A,2),1)]
G_B = [reshape(X_B,size(X_B,1)*size(X_B,2),1)...
reshape(Y_B,size(Y_B,1)*size(Y_B,2),1)...
reshape(Z_B,size(Z_B,1)*size(Z_B,2),1)]
figure; hold all;
scatter(G_A(:,1), G_A(:,2), 100, G_A(:,3),'s', 'filled');
scatter(G_B(:,1), G_B(:,2), 100, G_B(:,3),'s', 'filled'); colorbar;
Therefore, (assuming the dx and dy and end points are lined up correctly) we can see how the coordinates of "B" are a subset of the coordinates of "A". I would like to edit the G_B matrix so that it includes all of the G_A coordinates, but has NaN values in the coordinates where B's data doesn't originally exist.
I would be happy to elaborate or restate my problem if it is unclear.
Thank you in advance!

Accepted Answer

Kristin Busa
Kristin Busa on 20 Jan 2014
I failed to mention that I have replaced certain portions of the meshed grids with NaN's themselves, so I don't neccessarily end up with a rectangular grid for the subset or the main set. This seemed to work, though:
Coord_A = G_A(:,1:2);
Coord_B = G_B(:,1:2);
for i = 1:1:length(X_A)
[id(i),locb(i)] = ismember(Coord_B(i,:),Coord_A,'rows');
end
Rbig = nan(size(G_A));
Rbig(:,1:2) = G_A(:,1:2);
Rbig(locb,3) = Z_B;
This seemed to do the trick.

More Answers (1)

Walter Roberson
Walter Roberson on 20 Jan 2014
Quick cheat: interp2() without extrapolation.
If the grids are exactly aligned then you could be much more efficient: create the NaN matrix, find() the index of min(bdyx_B) in bdyx_A, find the index of max(bdyx_B) in bdyx_A, and copy the smaller data into that range of indices
P = nan(size(G_B));
P(11:16, 11:16) = G_A;

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!