Keeping the rows in an array with specified numbers
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Voss
2022-4-2
numbersTokeep = [5,6];
A = magic(5);
disp(A);
% keep rows where column 2 is 5 or 6:
A = A(ismember(A(:,2),numbersTokeep),:);
disp(A);
A = magic(5);
% or, delete rows where column 2 is not 5 or 6:
A(~ismember(A(:,2),numbersTokeep),:) = [];
disp(A);
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!