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

3 vues (au cours des 30 derniers jours)
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.

Réponse acceptée

Awais Saeed
Awais Saeed le 14 Déc 2021
Modifié(e) : Awais Saeed le 14 Déc 2021
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

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by