Matrix Subtraction by taking mean of same matrix
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I want to subtract of data which is of 413 X 264 and from taking a mean of same data with 45x45 matrix
My code is
winSz = [45,45]; % window size [width (x), height (y)]
[m, n] = size(HH_lin_0p2km);
winX0 = 1:winSz(1):n-winSz(1)+1; % starting index of x-values for each window
winY0 = 1:winSz(2):m-winSz(2)+1; % starting index of y-values for each window
xWin = 0:winSz(1)-1;
yWin = 0:winSz(2)-1;
for i = 1:size(HH_C,1)
for j = 1:size(HH_C,2)
HH_C = HH_lin_0p2km(winY0(i)+yWin, winX0(j)+xWin)-nanmean(HH_lin_0p2km(winY0(i)+yWin, winX0(j)+xWin),'all');
HV_C = HV_lin_0p2km(winY0(i)+yWin, winX0(j)+xWin)-nanmean(HV_lin_0p2km(winY0(i)+yWin, winX0(j)+xWin),'all');
end
end
And I am getting error as:
I am looking for output in 413x264 MATRIX
ndex exceeds the number of array elements. Index must not exceed 5.
Error in Soil_Moisture_Estimation (line 492)
HH_C = HH_lin_0p2km(winY0(i)+yWin, winX0(j)+xWin)-nanmean(HH_lin_0p2km(winY0(i)+yWin, winX0(j)+xWin),'all');
Any one please help
0 Kommentare
Akzeptierte Antwort
Matt J
am 16 Nov. 2021
Bearbeitet: Matt J
am 16 Nov. 2021
It would be faster to use sepblockfun from the File Exchange,
HH_C = process(HH_lin_0p2km);
HV_C = process(HV_lin_0p2km);
function out=process(H)
winSz = [45,45]; % window size [width (x), height (y)]
[m, n] = size(H);
mm=ceil(winSz(1)/m)*m;
nn=ceil(winSz(2)/n)*n;
if mm*nn>m*n, H(mm,nn)=0; end
nanmap=isnan(H);
H(nanmap)=0;
Means=sepblockfun(H,winSz,'sum')./sepblockfun(nanmap,winSz,'sum');
out=H-repelem(Means,winSz);
out(nanmap)=nan;
out=out(1:m,1:n);
end
2 Kommentare
Matt J
am 17 Nov. 2021
Bearbeitet: Matt J
am 17 Nov. 2021
How about the following modified version? I've run it on a 2x2 winSz as a test.
winSz=[2,2];
H=randi(4,4)
[H_C,blockMeans] = process(H,winSz)
function [out,Means]=process(H,winSz)
[m, n] = size(H);
mm=ceil(winSz(1)/m)*m;
nn=ceil(winSz(2)/n)*n;
if mm*nn>m*n, H(mm,nn)=0; end
nanmap=isnan(H);
H(nanmap)=0;
Means=sepblockfun(H,winSz,'sum')./sepblockfun(~nanmap,winSz,'sum');
Means=repelem(Means,winSz(1),winSz(2));
out=H-Means;
out(nanmap)=nan;
out=out(1:m,1:n);
end
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!