フィルターのクリア

Average values only next to each other in a matrix

1 回表示 (過去 30 日間)
Jimmy Neutron
Jimmy Neutron 2021 年 4 月 16 日
回答済み: David Hill 2021 年 4 月 16 日
I have a matrix as such: (it is much longer irl)
A = [ 62.9; 63; 83.10; 83; 43; 43; 63; 63.2; 82.9; 83; 83; 62.90; 62.90; 62.9 ];
I am looking for an efficient way to average the values that surround the values next to each that are almost identical ( +- 0.2) other as such:
A_final = [ 62.95; 83; 43; 63.1, 83; 62.90 ]
I was thinking to do it with unique, but then I risk the possibility of the same value coming up in matrix A. Is there a simple way to do so in matlab?
  2 件のコメント
the cyclist
the cyclist 2021 年 4 月 16 日
Step 1: Define "almost identical" mathematically.
Jimmy Neutron
Jimmy Neutron 2021 年 4 月 16 日
Added it :) it is plus minus 0.2

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

採用された回答

David Hill
David Hill 2021 年 4 月 16 日
I don't think there is an easy way, but a simple loop works.
A = [ 62.9; 63; 83.10; 83; 43; 43; 63; 63.2; 83; 83; 62.90; 62.90 ];
A_final=[];
b=A(1);
c=1;
for k=2:length(A)
if A(k)<1.1*b/c&&A(k)>.9*b/c
b=b+A(k);
c=c+1;
else
A_final=[A_final;b/c];
b=A(k);
c=1;
end
end
A_final=[A_final;b/c];

その他の回答 (1 件)

Ahmed Redissi
Ahmed Redissi 2021 年 4 月 16 日
You can reshape the n by 1 vector so that it is 2 by n/2 and then take the mean which will calculate the average for each column and give you the result that you want. Here is how it is done:
A = [62.9; 63; 83.10; 83; 43; 43; 63; 63.2; 83; 83; 62.90; 62.90];
n = length(A);
A_shaped = reshape(A,2,n/2);
A_final = mean(A_shaped)';

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by