How to select the first two (or first nth) rows of a matrix when the first and second elements of the rows always shows the same values fro more than two rows?

7 views (last 30 days)
I have a matrix A with 3 columns and multiple rows,
A =
a_1 b_1 7
a_1 b_1 8
a_1 b_1 4
a_2 b_2 2
a_2 b_2 5
a_2 b_2 3
a_2 b_2 2
a_3 b_3 9
a_3 b_3 8
a_3 b_3 12
a_3 b_3 25
a_3 b_3 10
... ... ...
I would like to make another matrix B that contains the first two rows of each couple of parameter (a_i, b_i) in A , such that
B =
a_1 b_1 7
a_1 b_1 8
a_2 b_2 2
a_2 b_2 5
a_3 b_3 9
a_3 b_3 8
... ... ...
Note that the value a_i and b_i ae not unique, but each couple (a_i, b_i) would be different from (a_j, b_j).
Thanks in advance!

Accepted Answer

Matt J
Matt J on 14 Feb 2022
Edited: Matt J on 14 Feb 2022
G=findgroups(A(:,1),A(:,2));
discard=cell2mat( splitapply(@(x){x(3:end,:)} , (1:numel(G))' ,G ) );
B=A;
B(discard,:)=[];

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!