Borrar filtros
Borrar filtros

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 visualizaciones (últimos 30 días)
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!

Respuesta aceptada

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

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by