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 个)

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), :)
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 的更多信息

标签

提问:

2017-7-24

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by