在matlab中的多列中查找多个重复值
Find more than a single repeating values in multiple columns in matlab
我已经尽力找到了这个问题的答案。
所以我有一个巨大的矩阵,A 列是 ID。 B 列和 C 列是确定的 values/scores。
像这样:
A B C
876 0 1
159 2 3
887 0 1
876 1 2
597 1 3
159 2 3
必填:
我希望 matlab 自动检测 id 是否再次出现在 A 列中。如果是,它应该查找 B 列和 C 列中的值并查看它们是否匹配。如果他们这样做,输出应该告诉 id,B 和 C 列中的 2 个值以及计数(事件)。
以上示例的输出应为:
A B C Count
159 2 3 2
有什么帮助吗?
试试这个。我修改了第一行以提供更好的示例:
data = [ 876 1 2
159 2 3
887 0 1
876 1 2
597 1 3
159 2 3 ];
[~, t, u] = unique(data, 'rows');
c = histc(u, 1:max(u));
ind = c>1;
result = [data(t(ind),:) c(ind)];
结果:
result =
159 2 3 2
876 1 2 2
使用 accumarray
的替代方法:(我添加了测试数据)。
clear
clc
close all
D = [876 0 1; 159 2 3; 887 0 1 ;876 1 2 ;597 1 3 ;159 2 3;876 0 1; 876 0 1];
%// Find unique rows and their indices
[x,~,z] = unique(D,'rows');
n=accumarray(z,1);
rowstokeep = find(n>1);
Out = [x(rowstokeep,:) n(rowstokeep,:)]
Out =
159 2 3 2
876 0 1 3
为了根据第 4 列对输出进行排序,请使用 sort
和 descend
选项,如下所示:
[~,Idx] = sort(Out(:,4),'descend')
Out = Out(Idx,:)
我已经尽力找到了这个问题的答案。
所以我有一个巨大的矩阵,A 列是 ID。 B 列和 C 列是确定的 values/scores。
像这样:
A B C
876 0 1
159 2 3
887 0 1
876 1 2
597 1 3
159 2 3
必填: 我希望 matlab 自动检测 id 是否再次出现在 A 列中。如果是,它应该查找 B 列和 C 列中的值并查看它们是否匹配。如果他们这样做,输出应该告诉 id,B 和 C 列中的 2 个值以及计数(事件)。
以上示例的输出应为:
A B C Count
159 2 3 2
有什么帮助吗?
试试这个。我修改了第一行以提供更好的示例:
data = [ 876 1 2
159 2 3
887 0 1
876 1 2
597 1 3
159 2 3 ];
[~, t, u] = unique(data, 'rows');
c = histc(u, 1:max(u));
ind = c>1;
result = [data(t(ind),:) c(ind)];
结果:
result =
159 2 3 2
876 1 2 2
使用 accumarray
的替代方法:(我添加了测试数据)。
clear
clc
close all
D = [876 0 1; 159 2 3; 887 0 1 ;876 1 2 ;597 1 3 ;159 2 3;876 0 1; 876 0 1];
%// Find unique rows and their indices
[x,~,z] = unique(D,'rows');
n=accumarray(z,1);
rowstokeep = find(n>1);
Out = [x(rowstokeep,:) n(rowstokeep,:)]
Out =
159 2 3 2
876 0 1 3
为了根据第 4 列对输出进行排序,请使用 sort
和 descend
选项,如下所示:
[~,Idx] = sort(Out(:,4),'descend')
Out = Out(Idx,:)