How do I compare each value and take the average of same number of every comparison in a vector?

3 ビュー (過去 30 日間)
I have a matrix A=[1124x1]. I want to check each element with the next element of the matrix A. If these elements are same then want the average value of them and result will be a matrix [Nx1] where N will be number of different element in the matrix A
for example A=[95; 95; 95; 96;96;96;97;97;97] and result will be [95;96;97].
Thank you
  2 件のコメント
Adam Danz
Adam Danz 2019 年 8 月 6 日
編集済み: Adam Danz 2019 年 8 月 6 日
"...result will be a matrix [Nx1] where N will be number of different element in the matrix A"
unique(A,'stable')
But that is not the average of those groups. Is the unique() function what you were looking for?
If the input/output pattern below if your goal, see the following solution.
A=[95;95;95;96;96;96;97;97;97;95;95;96]; %input
B=[95;96;97;95;96]; %output
idx = [true;diff(A)~=0];
B = A(idx);
the cyclist
the cyclist 2019 年 8 月 6 日
What should the output of
A = [95 95 96 96 97 97 95 95]
be?

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

回答 (2 件)

the cyclist
the cyclist 2019 年 8 月 6 日
Will identical values every be separated from each other? If not, then it seems like you just need
unique(A)
But this will not give you what you want if
A = [95 95 96 96 97 97 95 95]

Dave
Dave 2019 年 8 月 6 日
In which case if A = [95 95 96 96 97 97 95 95], then you could just sort(A) and then apply unique.
  2 件のコメント
the cyclist
the cyclist 2019 年 8 月 6 日
Unless what he actually wants as ouput in this case is
A = [95 96 97 95]
Adam Danz
Adam Danz 2019 年 8 月 6 日
Then it would merely be
A([true,diff(A)~=0])

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by