Resampling matrices in a loop using interp2

6 ビュー (過去 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
  1 件のコメント
Dinuka Kankanige
Dinuka Kankanige 2022 年 12 月 12 日
Thank you. This too is helpful.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInterpolation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by