Resampling matrices in a loop using interp2
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Dinuka Kankanige
le 3 Déc 2022
Commenté : Star Strider
le 12 Déc 2022
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
0 commentaires
Réponse acceptée
Star Strider
le 3 Déc 2022
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')
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 commentaires
Plus de réponses (1)
Matt J
le 3 Déc 2022
Déplacé(e) : Matt J
le 3 Déc 2022
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
Voir également
Catégories
En savoir plus sur Multirate Signal Processing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!