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