Resampling matrices in a loop using interp2
Show older comments
I'm trying to resample 200 matrices of 300*500 size to 30*50 using interp2. My objective is to load each resampled matrix into workspace after running the loop. In the code given here, it indicates there's an issue with how I use interp2 within the for-loop. Could someone please help me resolve this? Or any better approaches than this are also welcome.
-Thank you-
%filepath to directory where data is located
filepath='C:\Users\Task 1\';
%root part of filename (part that doesn't change each time)
filestem='VOD_Au_0dot1degree_';
for i=1:200
%loading files into a struct
A(i)=load(strcat(filepath,filestem,num2str(i),'.mat'));
%resampling
%new matrix column & row size
nc=50;nr=30;
%current matrix column & row size
c=500;r=300;
[C,R]=meshgrid(1:(c-1)/(nc-1):c,1:(r-1)/(nr-1):r);
downscaled_vod(i)=interp2(double(A(i).vod_monthly),C,R);
figure(i)=imagesc(downscaled_vod(i));
end
Accepted Answer
More Answers (1)
It is wasteful here to use meshgrid. Also, you should hoist C and R out of the the loop since they don't change with i:
%resampling
%new matrix column & row size
nc=50;nr=30;
%current matrix column & row size
c=500;r=300;
[C,R]=deal(1:(c-1)/(nc-1):c,1:(r-1)/(nr-1):r);
for i=1:200
%loading files into a struct
A(i)=load(strcat(filepath,filestem,num2str(i),'.mat'));
downscaled_vod{i}=interp2(double(A(i).vod_monthly),C,R);
figure(i)=imagesc(downscaled_vod{i});
end
1 Comment
Dinuka Kankanige
on 12 Dec 2022
Categories
Find more on Multirate Signal Processing 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!
