フィルターのクリア

Taking mean of values whose difference is smaller

1 回表示 (過去 30 日間)
BlueBee77
BlueBee77 2016 年 10 月 27 日
コメント済み: BlueBee77 2016 年 10 月 28 日
I have an array like [81,144,146,214,261,267,339,458,580]. In this array there are some elements whose difference is smaller e.g., 144 and 146 also 261 and 267. I want to have only one value from each either 144 or 146 and either 261 or 267. I thought of comparing the elements and if difference is smaller the find mean of the two values and put in another array and move to the next element for comparison. But the code is not giving the results which i want. I would appreciate if anyone can help. Below is my code:
for i=1:length(res)
sub= abs(res(i+1)-res(i))
if sub<40
Newres(i)= mean(res(i+1),res(i));
i= i+2;
else
Newres(i)= res(i);
end
end

採用された回答

Adam
Adam 2016 年 10 月 27 日
編集済み: Adam 2016 年 10 月 27 日
a = [81,144,146,214,261,267,339,458,580];
d = diff( a );
idx = find( d < 40 );
a( idx ) = ( a( idx ) + a( idx + 1 ) ) / 2;
a( idx + 1 ) = [];
  1 件のコメント
BlueBee77
BlueBee77 2016 年 10 月 28 日
Thanks alot, it solved my problem.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by