Keeping the rows in an array with specified numbers

조회 수: 2 (최근 30일)
L'O.G.
L'O.G. 2022년 4월 2일
답변: Voss 2022년 4월 2일
I want to keep the part of the array with specified numbers in column 2. This deletes the rows in the array with the specified numbers in column 2, but how do I do the opposite (keep all rows where the numbers 5 or 6 are in column 2)?
numbersTokeep = [5,6];
A = magic(5);
for n = numbersToKeep
A((A(:,2) == n),:) = [];
end

채택된 답변

Voss
Voss 2022년 4월 2일
numbersTokeep = [5,6];
A = magic(5);
disp(A);
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
% keep rows where column 2 is 5 or 6:
A = A(ismember(A(:,2),numbersTokeep),:);
disp(A);
23 5 7 14 16 4 6 13 20 22
A = magic(5);
% or, delete rows where column 2 is not 5 or 6:
A(~ismember(A(:,2),numbersTokeep),:) = [];
disp(A);
23 5 7 14 16 4 6 13 20 22

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by