Resampling matrices in a loop using interp2

조회 수: 2 (최근 30일)
Dinuka Kankanige
Dinuka Kankanige 2022년 12월 3일
댓글: Star Strider 2022년 12월 12일
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.
.
  댓글 수: 2
Dinuka Kankanige
Dinuka Kankanige 2022년 12월 12일
Thank you. This is helpful.
Star Strider
Star Strider 2022년 12월 12일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (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