## Moving mean in matrix

Guilherme Lopes de Campos

### Guilherme Lopes de Campos (view profile)

さんによって質問されました 2019 年 6 月 17 日

### dpb (view profile)

さんによって コメントされました 2019 年 6 月 17 日
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

#### 0 件のコメント

サインイン to comment.

R2012a

## 1 件の回答

2019 年 6 月 17 日

### dpb (view profile)

2019 年 6 月 17 日

% 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.

#### 2 件のコメント

Guilherme Lopes de Campos

### Guilherme Lopes de Campos (view profile)

2019 年 6 月 17 日
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),