Find average of only directly repeating values in array
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Niklas Hausmann
am 6 Dez. 2017
Kommentiert: Niklas Hausmann
am 7 Dez. 2017
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?
0 Kommentare
Akzeptierte Antwort
Andrei Bobrov
am 7 Dez. 2017
ii = [true;diff(A(:,2)) ~= 0];
out = [accumarray(cumsum(ii),A(:,1),[],@mean),A(ii,2)];
Weitere Antworten (2)
Akira Agata
am 7 Dez. 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));
Roger Stafford
am 7 Dez. 2017
Bearbeitet: Roger Stafford
am 7 Dez. 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 Kommentare
Image Analyst
am 7 Dez. 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.
Siehe auch
Kategorien
Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!