フィルターのクリア

Find average of only directly repeating values in array

3 ビュー (過去 30 日間)
Niklas Hausmann
Niklas Hausmann 2017 年 12 月 6 日
コメント済み: Niklas Hausmann 2017 年 12 月 7 日
I have an array with two columns, that I want to clean up by finding the average value of column 1 for each unique value in column 2.
A = [
1 0.1
2 0.2
3 0.2
4 0.4
5 0.2 ]
I tried the unique function but have problems with repeating values (here 0.2 in the last row), so that in the above example I would not just calculate the average of rows 2 and 3, but of 2, 3 and 5. Is there a way to calculate the average of rows 2 and 3 separately from row 5?

採用された回答

Andrei Bobrov
Andrei Bobrov 2017 年 12 月 7 日
ii = [true;diff(A(:,2)) ~= 0];
out = [accumarray(cumsum(ii),A(:,1),[],@mean),A(ii,2)];
  1 件のコメント
Niklas Hausmann
Niklas Hausmann 2017 年 12 月 7 日
This hits the spot! Thank you all very much!

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

その他の回答 (2 件)

Akira Agata
Akira Agata 2017 年 12 月 7 日
If you have the Image Processing Toolbox, the following code can do that task.
% Sample data
A = [1 0.1;
2 0.2;
3 0.2;
4 0.4;
5 0.2;
6 0.2;
7 0.3];
% Find directly repeating values and assign group ID
idx = diff(A(:,2)) == 0;
idx = [idx; 0] | [0; idx];
group = bwlabel(idx);
% Calculate the average for each group ID
average = splitapply(@mean, A(idx,1), group(idx));
  1 件のコメント
Niklas Hausmann
Niklas Hausmann 2017 年 12 月 7 日
Thank you, this helped a lot. But ideally the code would also create unique labels for each value that is different to its predecessor.
As I understand it, the code above focuses on groups of identical values and leaves out others that are unique and only occur once.

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


Roger Stafford
Roger Stafford 2017 年 12 月 7 日
編集済み: Roger Stafford 2017 年 12 月 7 日
[~,~,n] = unique(A(:,2));
av = accumarray(n,A(:,1),[],@mean);
Note: You should be careful about how the elements in second column of A are generated. Different methods which produce fractions can result in tiny differences due to different round-off errors in what would ordinarily be regarded as like amounts. These would appear in different groups in 'unique'.
As to your question, "Is there a way to calculate the average of rows 2 and 3 separately from row 5?", are you asking a second question here distinct from the first one, or are you somehow trying to figure how to achieve that first objective?
  2 件のコメント
Image Analyst
Image Analyst 2017 年 12 月 7 日
Yeah, Niklas, and "each unique value" seems contradictory to "only directly repeating values". So which is it? What about .1 and .4? They are unique values but not repeated values, so how do you want to handle those? Do you want those values in the output or not? Please clarify.
Niklas Hausmann
Niklas Hausmann 2017 年 12 月 7 日
Your are right, I am lacking the correct words here for values that are unique not only because of their value but also because of their grouping.
So the ideal outcome would include the averages for .1 and .4 as well as the ones for each group of 0.2:
ans =
[1 0.1;
2.5 0.2;
4 0.4;
5 0.2];

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by