Moving mean in matrix

2 次查看(过去 30 天)
Guilherme Lopes de Campos
评论: 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
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 中查找有关 Dictionaries 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by