Average Multiple Matrix Columns Based On Duplicate Entries In A Different ColumnVector
4 次查看(过去 30 天)
显示 更早的评论
I have a column vector consisting of hundreds of unit64 values where duplicates appear and are always grouped together but groups appear only once. Each element of this vector corresponds to a row in a separate 2D matrix of double values. I would like to remove the duplicates from the row vector and then remove the corresponding rows in the matrix, replacing them with a single row where each column is average the removed elements in the column, while keeping the order stable. So with data like this:
1
1
2
A= 2
3
3
1 2 3
2 3 4
3 4 5
B = 4 5 6
5 6 7
6 7 8
I would want the output to be
1.5 2.5 3.5
C = 3.5 4.5 5.5
5.5 6.5 7.5
Some combination of unique and accumarray seems to be in order but I cannot figure out how to handle the multiple coulmn part of the problem.
0 个评论
采纳的回答
Guillaume
2018-7-13
编辑:Guillaume
2018-7-13
So if I understood correctly, the shape of B does not matter and it is to be considered a column vector.
[~, ~, subs] = unique(A, 'stable');
C = accumarray(subs, B(:), [], @mean)
Note that whether or not the groups appear only once does not matter for the above. All the values with the same corresponding A still get averaged.
Also note that if A is already integer values from 1 to n with no gap and in the right order, then the call to unique is unnecessary and you can just pass A instead of subs.
6 个评论
Guillaume
2018-7-16
编辑:Guillaume
2018-7-16
C = splitapply(@(m) mean(m, 1), B, g);
should fix the problem. If a group only contain one row, the mean will be taken along the columns of the one row instead of across the rows as it happens for matrices. As a result, you'd get a scalar value instead of a row, hence the Dimensions of arrays being concatenated are not consistent. @(m) mean(m, 1) forces the mean to be taken across the rows regardless.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!