Moving mean in matrix

조회 수: 3 (최근 30일)
Guilherme Lopes de Campos
Guilherme Lopes de Campos 2019년 6월 17일
댓글: dpb 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

답변 (1개)

dpb
dpb 2019년 6월 17일
편집: dpb 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)
ERRATUM/ADDENDUM:
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 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),
Could help me please?
Guilherme
dpb
dpb 2019년 6월 17일
See ADDENDUM to Answer -- altho why not use the ismissing/fillmissing pair (unless you are on an earlier release, maybe?)?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품


릴리스

R2012a

Community Treasure Hunt

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

Start Hunting!

Translated by