Separate duplicates into different numeric matrices
显示 更早的评论
I have a numeric matrix with duplicate values and want to split the first set from the second set. For example, my matrix looks like this:
100 1 0.44
100 2 0.12
100 3 1.86
100 1 0.02
100 2 3.29
100 3 0.39
101 1 0.32
101 2 0.29
101 3 0.59
101 1 0.00
101 2 0.99
101 3 0.39
I would like to create two matrices that look like the following:
Matrix 1
100 1 0.44
100 2 0.12
100 3 1.86
101 1 0.32
101 2 0.29
101 3 0.59
Matrix 2
100 1 0.02
100 2 3.29
100 3 0.39
101 1 0.00
101 2 0.99
101 3 0.39
回答(2 个)
Guillaume
2017-7-24
m = [100 1 0.44
100 2 0.12
100 3 1.86
100 1 0.02
100 2 3.29
100 3 0.39
101 1 0.32
101 2 0.29
101 3 0.59
101 1 0.00
101 2 0.99
101 3 0.39];
[~, iunique] = unique(m(:, [1 2]), 'rows'); %optionally add 'first' or 'last' flag
m1 = m(iunique, :)
m2 = m(setdiff(1:size(m, 1), iunique), :)
Andrei Bobrov
2017-7-24
a = [100 1 0.44
100 2 0.12
100 3 1.86
100 1 0.02
100 2 3.29
100 3 0.39
101 1 0.32
101 2 0.29
101 3 0.59
101 1 0.00
101 2 0.99
101 3 0.39];
b = sortrows(a,1:2);
out = {b(1:2:end,:);b(2:2:end,:)};
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!