Identify the minimum number of rows in a matrix meeting a condition
Mostrar comentarios más antiguos
Hi everyone,
Please assume that I have a matrix as follows:
A = [0 1 0 0
1 1 0 0
0 0 1 0
0 0 0 1
1 0 1 1]
I would like to identify and selcet the minimum number of rows that have value of one in all columns (altoghether). For example, rows 2 and 5 are the best choice for matrix A. Do you have any suggestion?
Regards,
Amir
2 comentarios
What about this case?
A = [1 1 0 0
0 0 0 1
0 0 1 1]
You could argue that A([1,3],:) is the minimal selection because only those two rows contain a cluster of two [1,1] whereas all three rows contain the single-element cluster [1], possibly as sub-clusters of the [1,1] sequences.
On the other hand, if you make the rule that a cluster of length n cannot belong to a longer cluster of length m>n and can therefore be neighbored only by zeros, then A(2,:) is the minimal selection because now it is the only row qualifying as having a single-element cluster [1].
Amirhossein Moosavi
el 8 de Jul. de 2020
Respuesta aceptada
Más respuestas (2)
madhan ravi
el 8 de Jul. de 2020
Editada: madhan ravi
el 8 de Jul. de 2020
According to what I understood, see if this does what you want to do:
[a, b] = unique(A, 'rows', 'stable');
s = sum(a, 2);
rows = b(s > 1)
1 comentario
Amirhossein Moosavi
el 8 de Jul. de 2020
Amirhossein Moosavi
el 8 de Jul. de 2020
2 comentarios
My method is faster for larger problems. For this matrix,
A = randi([0,1],40,100);
I obtain these timings,
Elapsed time is 0.193646 seconds.%Matt J
Elapsed time is 0.367822 seconds.%Amir
Amirhossein Moosavi
el 8 de Jul. de 2020
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!