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
回答(1 个)
KSSV
2020-8-19
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) ;
类别
在 帮助中心 和 File Exchange 中查找有关 Interpolation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!