Resampling matrices in a loop using interp2

4 次查看(过去 30 天)
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

采纳的回答

Star Strider
Star Strider 2022-12-3
I am not certain what hte problem is, since the error messagd is not part of the provided information.
I suspect the problem is in saving a matrix to a scalar, defined by:
downscaled_vod(i)
That can be solved by saving it to a cell array element:
downscaled_vod{i}
The same sort of problem could be due to the ‘double(A(i).vod_monthly)’ reference, although without more information, I cannot determine that.
A = exp(-((1:300).'-150).^2/3000) * exp(-((1:500)-250).^2/5000); % Create Matrix To Be Interpolated
figure
surf(A, 'EdgeColor','none')
colormap(turbo)
title('Original')
i = 1;
nc=50;
nr=30;
c=500;
r=300;
rv = linspace(1,r,nr);
cv = linspace(1,c,nc);
[C,R] = meshgrid(cv, rv);
downscaled.vod{i} = interp2(A, C, R);
figure
surf(downscaled.vod{i})
colormap(turbo)
title('Downscaled')
Using linspace to create the interpolation vectors is likely more efficien than the current code.
Also, put all these:
nc=50;
nr=30;
c=500;
r=300;
rv = linspace(1,r,nr);
cv = linspace(1,c,nc);
[C,R] = meshgrid(cv, rv);
before the loop, because they don't change in loop iterations. There is no need to create them in each iteration.
.

更多回答(1 个)

Matt J
Matt J 2022-12-3
移动:Matt J 2022-12-3
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

类别

Help CenterFile Exchange 中查找有关 Multirate Signal Processing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by