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

5 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Awais Saeed
Awais Saeed il 14 Dic 2021
Modificato: Awais Saeed il 14 Dic 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

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by