How to delete rows in matrix, where a value is repeated 3 times or more!

조회 수: 6 (최근 30일)
I have the matrix like this:
b =[
1 1 3 6 6 6 6; 1 1 3 6 6 6 7; 1 1 3 6 6 6 8; 1 1 3 6 6 6 9;
1 1 3 6 6 7 7; 1 1 3 6 6 7 8; 1 1 3 6 6 7 9; 1 1 3 6 6 8 8;
1 1 3 6 6 8 9; 1 1 3 6 6 9 9; 1 1 3 6 7 7 7]
I would like to remove all rows, where any value is repeated more than twice. In case of this matrix the rows 1,2,3,4 and 11 should be removed.

채택된 답변

Awais Saeed
Awais Saeed 2021년 12월 14일
편집: Awais Saeed 2021년 12월 14일
b =[ 1 1 3 6 6 6 6; 1 1 3 6 6 6 7; 1 1 3 6 6 6 8; 1 1 3 6 6 6 9;
1 1 3 6 6 7 7; 1 1 3 6 6 7 8; 1 1 3 6 6 7 9; 1 1 3 6 6 8 8;
1 1 3 6 6 8 9; 1 1 3 6 6 9 9; 1 1 3 6 7 7 7]
b = 11×7
1 1 3 6 6 6 6 1 1 3 6 6 6 7 1 1 3 6 6 6 8 1 1 3 6 6 6 9 1 1 3 6 6 7 7 1 1 3 6 6 7 8 1 1 3 6 6 7 9 1 1 3 6 6 8 8 1 1 3 6 6 8 9 1 1 3 6 6 9 9
delRow = [];
for row = 1:1:size(b,1)
x = b(row,:);
[~,~, counts] = unique(x);
counts = accumarray(counts,1).';
if (max(counts)>=3) % if number is repeated more than twice then
delRow = [delRow row]; % save row numbers and delete them later
end
end
b(delRow,:)=[] % delete those row now
b = 6×7
1 1 3 6 6 7 7 1 1 3 6 6 7 8 1 1 3 6 6 7 9 1 1 3 6 6 8 8 1 1 3 6 6 8 9 1 1 3 6 6 9 9

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by