How to find a unexpected high value and turn it to zero?

1 回表示 (過去 30 日間)
Masoud Taleb
Masoud Taleb 2020 年 4 月 26 日
コメント済み: Les Beckham 2020 年 4 月 27 日
Hello
I have a matrix with 4 columns. I want to code it in a way that column 4th to be checked by matlab and unwanted high value (in this example: 80) to be descovered and changes to zero. At the end, this matrix to be saved as csv file. (Since data are experimental, there is no rule to find this kind of error values in 4th column. I can say, it is at least 3 times bigger than avareage value of the column)
My current code works, but want to know if there is a better way to handel this? Maybe my loop is incorrect. I will be grateful if someone can help in this.
A = [2 3 4 2;5 6 7 3;8 9 0 6; 5 6 1 80;2 3 8 5;3 5 13 4]
as = length (A(:,4))
M = mean (A(:,4))
for i = 1:as
Max = max (A(:,4))
if Max > 3*M
A(A==Max)=0
else end
end
fname = sprintf('new.csv');
csvwrite (fname,A); % Write to CSV file

採用された回答

Les Beckham
Les Beckham 2020 年 4 月 26 日
編集済み: Les Beckham 2020 年 4 月 26 日
Here is a way to do this without a loop (using logical indexing)
A = [2 3 4 2;5 6 7 3;8 9 0 6; 5 6 1 80;2 3 8 5;3 5 13 4]
idx = A(:,4) > mean(A(:,4))*3; % determine indices of outliers
A(idx,4) = 0; % set outliers equal to zero
fname = sprintf('new.csv');
csvwrite (fname,A); % Write to CSV file
You can also use the isoutlier function:
A(isoutlier(A(:,4)), 4) = 0;
  2 件のコメント
Masoud Taleb
Masoud Taleb 2020 年 4 月 26 日
Perfect. Thanks for nice way :)
Les Beckham
Les Beckham 2020 年 4 月 27 日
You are welcome. Glad I could help.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by