Identify the minimum number of rows in a matrix meeting a condition
Show older comments
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 Comments
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
on 8 Jul 2020
Accepted Answer
More Answers (2)
madhan ravi
on 8 Jul 2020
Edited: madhan ravi
on 8 Jul 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 Comment
Amirhossein Moosavi
on 8 Jul 2020
Amirhossein Moosavi
on 8 Jul 2020
2 Comments
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
on 8 Jul 2020
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!