Find average of only directly repeating values in array
2 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Andrei Bobrov
2017 年 12 月 7 日
ii = [true;diff(A(:,2)) ~= 0];
out = [accumarray(cumsum(ii),A(:,1),[],@mean),A(ii,2)];
その他の回答 (2 件)
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));
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
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.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!