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?

4 次查看(过去 30 天)
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!

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by