根据一列对矩阵行进行分组 [英] Grouping matrix rows in terms of one column
本文介绍了根据一列对矩阵行进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
首先,我很难很好地描述问题,但我会尽力的.
First of all it is really hard for me to describe the problem really good but I'll try.
说我们有矩阵A
A = [23 1;
45 1
78 1
86 1
98 2
1 2
23 2
14 3
15 4
85 4]
我想要的输出是
B{1} = [23,45,78,86]
B{2} = [98,1,23]
B{3} = [14]
B{4} = [15,85]
请记住,原始A是一个巨大的矩阵,我不想使用for循环来做到这一点.我想使用使用并行处理的函数.
Bear in mind that the original A is a huge matrix, and I do not wanna do this with for loops. I would like to use functions that uses parallel processing.
推荐答案
您可以在此处使用accumarray
:
B = accumarray(A(:,2),A(:,1),[],@(x){x},{});
如果您知道A
已排序,并且第二列中没有缺失的条目,则也可以使用mat2cell
:
If you know that A
is sorted, and that there is no missing entry from the second column, you can also use mat2cell
:
counts = histc(A(:,2),unique(A(:,2)));
B = mat2cell(A(:,1),counts);
这篇关于根据一列对矩阵行进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文