# Moving mean in matrix

11 views (last 30 days)
Guilherme Lopes de Campos on 17 Jun 2019
Commented: dpb on 17 Jun 2019
Hi MATLAB community,
I am using the following code to identify number of 999999, replace to mean of column,
% Identify the columns that contain at least one 999999
isKey = matriz_media == key;
colIdx = any(isKey,1);
% Count the number of rows per column that are not 999999
rowCount = sum(~isKey);
% Temporarily replace 999999 with 0 and calculate the column means
matrixTemp = matriz_media .* ~isKey;
colMean = sum(matrixTemp)./rowCount;
colMean=transpose(colMean);
[rowIdx,colIdx] = find(matrixTemp==0);
matrixTemp(sub2ind(size(matrixTemp),rowIdx,colIdx)) = colMean(colIdx);
But, I need replace to moving mean of element previous and posterior, such as:
The element of column 5 and line 298 is 999999, I need replace this value to moving mean between (column 5 and element 297 and 299).
Thank you very much!
Guilherme

dpb on 17 Jun 2019
Edited: dpb on 17 Jun 2019
% Identify the columns that contain at least one 999999
isKey = matriz_media == key;
colIdx = find(any(isKey,1)); % turn into the column IDs from logical vector
% Replace key value with mean each side of missing value
for c=colIdx
ix=find(isKey(:,c)); % missing rows
matriz_media(ix,c)=interp1(matriz_media(:,c),matriz_media(ix,c)); % fill in with linear interpolation
end
Alternatively, w/ R2016b and later...
matriz_media(ismissing(matriz_media,key))=nan; % create missing values
matriz_media=fillmissing(matriz_media,'movmean',2); % fill in 2-pt movinvg mean (same as linear interp)
Oh, yeah...one oversight -- the column vector (:,c) contains all the values including the NaN and interp1 will end up with the same index...you've got to build an x vector without those elements to fill in with the wanted...let's see...
key=999999;
isKey = matriz_media == key;
colIdx = find(any(isKey,1)); % turn into the column IDs from logical vector
nRow=size(matriz_mdia,1); % number rows in array
% Replace key value with mean each side of missing value
for c=colIdx
ix=find(isKey(:,c)); % missing rows
x=1:nRow; % all x
x=x(~ix); % don't include missing ones
matriz_media(ix,c)=interp1(x,matriz_media(:,c),matriz_media(ix,c)); % fill in with linear interpolation
end
Z = zscore(matriz_media);
ALERT: Aircode; untested written in forum--check well. :)
The difference is using the x argument as well as y in interp1 which is all indices in array excepting the missing values to interpolate.

Guilherme Lopes de Campos on 17 Jun 2019
Hi dpb,
Thank for help,
But, not works,
key=999999;
isKey = matriz_media == key;
colIdx = find(any(isKey,1)); % turn into the column IDs from logical vector
% Replace key value with mean each side of missing value
for c=colIdx
ix=find(isKey(:,c)); % missing rows
matriz_media(ix,c)=interp1(matriz_media(:,c),matriz_media(ix,c)); % fill in with linear interpolation
end
Z = zscore(matriz_media);
Those value stayed of (NaN),
Guilherme
dpb on 17 Jun 2019
See ADDENDUM to Answer -- altho why not use the ismissing/fillmissing pair (unless you are on an earlier release, maybe?)?

R2012a

### Community Treasure Hunt

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

Start Hunting!