필터 지우기
필터 지우기

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)];

추가 답변 (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 CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by