Clear Filters
Clear Filters

Find average of only directly repeating values in array

3 views (last 30 days)
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?

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 7 Dec 2017
ii = [true;diff(A(:,2)) ~= 0];
out = [accumarray(cumsum(ii),A(:,1),[],@mean),A(ii,2)];

More Answers (2)

Akira Agata
Akira Agata on 7 Dec 2017
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 Comment
Niklas Hausmann
Niklas Hausmann on 7 Dec 2017
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.

Sign in to comment.


Roger Stafford
Roger Stafford on 7 Dec 2017
Edited: Roger Stafford on 7 Dec 2017
[~,~,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 Comments
Image Analyst
Image Analyst on 7 Dec 2017
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 on 7 Dec 2017
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];

Sign in to comment.

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!