Mean and identification values

6 ビュー (過去 30 日間)
Guilherme Lopes de Campos
Guilherme Lopes de Campos 2019 年 2 月 22 日
Hi Matlab Community,
I would to identify a specific number in an array, and change its value to the average of the previous and later data, I wrote the follow code, but it shows the error:
d= table2array(d);
[matriz_media,q,instrumento,ano_ini] = gerar_medias(d54);
matriz_media = matriz_media(1:q,3:instrumento);
X = matriz_media;
[l,c]=size(matriz_media)
for w=1:c
for q = 1:l
if matriz_media(q,w)==999999 % Identify this number in the matrix
matriz_media(q,w)= mean([matriz_media((q-1),(w-1)),matriz_media((q+1),(w+1))]); % Change the number 999999 to mean between element previous and subsequent.
end
end
end
Index in position 2 is invalid. Array indices must be positive integers or logical values.
Error in DEPURACAO_SEGUNDA_FASE (line 13)
matriz_media(q,w)= mean([matriz_media((q-1),(w-1)),
matriz_media((q+1),(w+1))]);
Thank you very much
Guilherme

採用された回答

Steven Lord
Steven Lord 2019 年 2 月 22 日
There's no such thing as element 0, row 0, or column 0 in a matrix in MATLAB.
If you replace your 999999 values with NaN you could use fillmissing with the 'movmean' method, at least assuming you intend "previous and later data" to refer to the previous and next data point in a particular row or column not along a diagonal as your code currently computes. As an example, make some sample data.
rng default
x = randi(10, 1, 10)
Change some of the values in x into NaN (missing values.)
x([4 7]) = NaN
Take the moving mean with a window of 1 element before and after the current element.
result = fillmissing(x, 'movmean', [1 1])
Element 4 of result is the average of elements 3 and 5 of x since element 4 of x is missing.
Element 7 of result is the average of elements 6 and 8 of x since element 7 of x is missing.
The remaining elements in result are the same as the corresponding (non-missing) elements of x.
  1 件のコメント
Guilherme Lopes de Campos
Guilherme Lopes de Campos 2019 年 2 月 22 日
It works correctly,
Thank you very much for attention,
I am very grateful
Guilherme Lopes

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by