2D Mesh interpolation or calculation effeciency

My question therefore partains code effeciency and readability of the below code. I have a function that uses values the Big mesh to interpolate values in a Small mesh. The Big mesh coordinates are outputs from a diffent calculation. The new outputs should be consistent to the Small Mesh coordinates.
[S1,S2]=meshgrid(0:1:30,40:1:60); % Small Mesh
[B1,B2]=meshgrid(0:10:30,40:10:60); % Big Mesh
M=size(B1,1);
N=size(B1,2);
cell_ij={S1;S2};
spacing=unique(diff(S2));
for i=1:M;
for j=1:N;
if i~=j
try
Xs1=find(unique(cell_ij{1,1})==B1(i,j));
Ys1=find(unique(cell_ij{2,1})==B2(i,j));
Xs2=find(unique(cell_ij{1,1})==B1(i,end));
Ys2=find(unique(cell_ij{2,1})==B2(i,end));
dist=sqrt((Xs1-Xs2)^2+(Ys1-Ys2)^2);
if dist>0
xcords=Xs1:spacing:Xs2
ycords=Ys1:spacing:Ys2
% the rest of the calculations using the coordinates
% new_outputs(i,j)=cal_function(xcords,ycords);
else
disp('too close')
end
catch
disp('skip')
end
end
end
end

Answers (1)

You need not to find the indices.....you have to do interpolation. Read about interp2.
If X, Y, Z are your bigger grid data. And Xi, Yi are smaller grids, which are subset to X, Y.
Zi = interp2(X,Y,Z,Xi,Yi) ;

4 Comments

Mau
Mau on 19 Aug 2020
Edited: Mau on 19 Aug 2020
Just a clarification that the big and small mesh have different sizes. Additionally, the I intend to compare all index points from the small mesh with the rest of the mesh. I think that the interp2 considers closer points as opposed to an entire mesh.
The dimensions doesnt matter. You go ahead with interp2.
My input.
[S1,S2]=meshgrid(0:1:30,40:1:60); % Small Mesh
[B1,B2]=meshgrid(0:10:30,40:10:60); % Big Mesh
distB=sqrt((B1-B1).^2+(B2-B2).^2);
distS=interp2(B1,B2,distB,S1,S2);
Output.
all zeros
Thanks.
Please note that your distB also has all zeros.
[S1,S2]=meshgrid(0:1:30,40:1:60); % Small Mesh
[B1,B2]=meshgrid(0:10:30,40:10:60); % Big Mesh
distB=sqrt((B1).^2+(B2).^2);
distS=interp2(B1,B2,distB,S1,S2);

Sign in to comment.

Categories

Find more on Interpolation in Help Center and File Exchange

Products

Release

R2018b

Asked:

Mau
on 19 Aug 2020

Commented:

on 19 Aug 2020

Community Treasure Hunt

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

Start Hunting!