我有一个输入矩阵如下
all = [12 16;12 13;8 14;14 19;3 6;8 6;13 25;25 14;7 2];
我需要以下输出
output = [12 16;8 14;3 6;13 25;7 2];
输出的解释如下。
输入的第一行,即 12 16
是输出的第一行,因为这两个数字之前在输出矩阵中从未重复过(显然)。
不需要第二行输入,即 12 13
,因为数字 12
出现在第一行输出中,即重复
输入的第三行,即 8 14
是输出的第二行,因为这两个数字以前从未在输出矩阵中重复过。
不需要第四行输入,即 14 19
,因为数字 14
出现在输出中,即重复
类似的路线
需要 3 6
,因为两者不重复,
8 6
不需要,因为 8
和 6
都是重复的,
13 25
需要,因为两者不重复
25 14
不需要,因为两者都是重复的
7 2
需要,因为两者不重复
我无法得到任何开始的想法。任何帮助将不胜感激。
谢谢!
最佳答案
单衬解决方案
res = all(arrayfun(@(ii) isempty(intersect(all(1:ii-1,:),all(ii,:))),1:size(all,1)),:);
结果
res =
12 16
8 14
3 6
7 2
说明
让我们将一行代码分成更详细且有文档记录的代码块:
%defines a function which validates for each index wheter the row is
%completely unique are not.
uniqueRowIndicator = @(ii) isempty(intersect(all(1:ii-1,:),all(ii,:)));
%finds all the unique row in the matrix
inds = arrayfun(uniqueRowIndicator,1:size(all,1));
%extracts the result from the returned indices
res = all(inds,:);
关于matlab - 如何在matlab中获得唯一的数字对,其中两个数字之前在矩阵中没有重复过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37973349/