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

5 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Awais Saeed
Awais Saeed el 14 de Dic. de 2021
Editada: Awais Saeed el 14 de Dic. de 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

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by