필터 지우기
필터 지우기

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 (최근 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에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by