我一直试图用一种聪明的方式来做这件事。给定具有以下结构的矩阵(或单元格):
A = [-1 1
-1 2
1 3
3 5
2 3
2 4
2 7
4 5
5 6
6 7
7 -2 ]
(请注意,上述矩阵/单元格在两列中都未排序,并且包含负数)。
如何根据特定列的唯一值对其进行分组。例如。第二列分组所需的输出类似于:
B{1} = [-1]
B{2} = [-1]
B{3} = [1,2]
B{4} = [2]
B{5} = [3,4]
B{6} = [5]
B{7} = [2,6]
B{-2} = [7]
提前致谢!
答案 0 :(得分:6)
您可以使用accumarray
:
[~,~,subs] = unique(A(:,2));
values = accumarray(subs,A(:,1),[],@(x) {x});
ofGroup = accumarray(subs,A(:,2),[],@(x) {x(1)});
out = [ofGroup values]
out =
[-2] [ 7]
[ 1] [ -1]
[ 2] [ -1]
[ 3] [2x1 double]
[ 4] [ 2]
[ 5] [2x1 double]
[ 6] [ 5]
[ 7] [2x1 double]
如果您 真的 坚持您的建议订单,您可以执行以下操作,但我认为不应该这样做。
% positives
pos = A( A(:,2) >= 0 , :);
[~,~,subs] = unique(pos(:,2));
posvalues = accumarray(subs,pos(:,1),[],@(x) {x});
posofGroup = accumarray(subs,pos(:,2),[],@(x) {x(1)});
% negatives
neg = A( A(:,2) < 0 , :);
[~,~,subs] = unique(neg(:,2));
negvalues = flipud( accumarray(subs,neg(:,1),[],@(x) {x}) );
negofGroup = flipud( accumarray(subs,neg(:,2),[],@(x) {x(1)}) );
out = [posofGroup posvalues; negofGroup negvalues ]
out =
[ 1] [ -1]
[ 2] [ -1]
[ 3] [2x1 double]
[ 4] [ 2]
[ 5] [2x1 double]
[ 6] [ 5]
[ 7] [2x1 double]
[-2] [ 7]
答案 1 :(得分:5)
怎么样:
[group, ~, subs] = unique(A(:,2))
B = accumarray(subs, A(:,1), [], @(x){x'})
结果
B=
[ 7]
[ -1]
[ -1]
[2,1]
[ 2]
[4,3]
[ 5]
[2,6]
和group
将B
的索引与其代表的组号相匹配
此外,如果您附上订购,那么您可以这样做:
[group, ~, subs] = unique(A(end:-1:1,2), 'stable');
B = flipud(accumarray(subs, A(end:-1:1,1), [], @(x){x'}));
group = flipud(group);
B =
[ -1]
[ -1]
[1x2 double]
[ 2]
[1x2 double]
[ 5]
[1x2 double]
[ 7]
group =
1
2
3
4
5
6
7
-2