Identify the minimum number of rows in a matrix meeting a condition
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
Matt J
2020-7-8
编辑:Matt J
2020-7-8
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].
采纳的回答
Matt J
2020-7-8
编辑:Matt J
2020-7-8
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 个评论
Matt J
2020-7-8
编辑:Matt J
2020-7-8
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
更多回答(2 个)
madhan ravi
2020-7-8
编辑:madhan ravi
2020-7-8
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
2020-7-8
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!