Identify the minimum number of rows in a matrix meeting a condition
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Amirhossein Moosavi
le 8 Juil 2020
Modifié(e) : Matt J
le 8 Juil 2020
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 commentaires
Matt J
le 8 Juil 2020
Modifié(e) : Matt J
le 8 Juil 2020
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].
Réponse acceptée
Matt J
le 8 Juil 2020
Modifié(e) : Matt J
le 8 Juil 2020
This solution uses the Optimization Toolbox, although I am still only half certain what row-selection rule you are looking for (see my question here).
[m,n]=size(A);
irp=1:m;
rp=randperm(m); %row permutation
irp(rp)=irp; %inverse permutation
Ap=A(rp,:);
x=optimvar('x',[1,m],'LowerBound',0,'UpperBound',1,'Type','integer');
prob=optimproblem('Objective',sum(x));
prob.Constraints=(x*Ap>=ones(1,n));
sol=solve(prob);
selection=find(round(sol.x(irp)));
2 commentaires
Matt J
le 8 Juil 2020
Modifié(e) : Matt J
le 8 Juil 2020
2) Just randomly pre-permute the rows of A (see my edited version above).
1) I find that my version is faster than yours for larger problems even when adding in the above permutation. For example, with A = randi([0,1],40,100), I find
Elapsed time is 0.193646 seconds.%Matt J
Elapsed time is 0.367822 seconds.%Amir
Plus de réponses (2)
madhan ravi
le 8 Juil 2020
Modifié(e) : madhan ravi
le 8 Juil 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)
Amirhossein Moosavi
le 8 Juil 2020
Voir également
Catégories
En savoir plus sur Elementary Math 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!