Solving overdetermined linear sytem for possible solutions
Show older comments
I have a coefficient Matrix A with the size N x M with values 0 or 1 and a Vector B with the size 1 x M. I'm searching for one or multiple possible solutions for C, a vector with the length K, which statisfy sum(A(C,:),1) == B. There are not infinite solutions, however at least 1 solutions exists.
Simplified example:
A = [1 0 1; 1 1 1; 1 0 0; 0 0 1]
B = [2 1 2]
A solution for C with K = 2 could be
C2 = [1 2];
C2l = logical([1 1 0 0]); % or as logical indexing
And for C with the K = 3 could be
C3 = [2 3 4];
C3l = logical([0 1 1 1]); % or as logical indexing
sum(A(C2l,:),1) == B
sum(A(C3,:),1) == B
I have access to optimisation toolbox, but I don't know how to formulate this constraint.
K = 3;
prob = optimproblem;
RI = optimvar('RowIndex',K,'Type','integer','LowerBound',0);
%% Constraints
prob.Constraints.colsum = sum(A(RI,:),1) == B;
I'm open for any approaches.
Answers (2)
Choose RI as a binary integer vector (0 or 1) of length N. Constraint is sum(RI) = K and sum(RI.'*A) = B.
Of course, this is only for the case that you know an RI such that sum(RI.'*A) = B exists.
1 Comment
Toy example
N=5;M=4;
A=randi([0,1],N,M);
K = 3;
B=sum(A(3:5,:)); %Known solution RI=[3,4,5]
prob = optimproblem;
X = optimvar('X', [1 size(A,1)], 'Type','integer','LowerBound',0, 'UpperBound', 1);
Constraint1 = sum(X,2)==K;
Constraint2 = X*A == B;
prob.Constraints = [Constraint1 Constraint2];
sol = solve(prob);
C = find(round(sol.X))
Download minL1intlin,
and solve min.
subject to sum(c)=K, c(i)∈{0,1}..
subject to sum(c)=K, c(i)∈{0,1}..N=5;M=4;
A=randi([0,1],N,M);
K = 3;
B=sum(A(3:5,:)); %Known solution RI=[3,4,5]
c=minL1intlin(A.',B.',1:N, [],[],ones(1,N),K,zeros(N,1),ones(N,1) );
RI=find(round(c))
7 Comments
Sergej Gein
on 2 Sep 2022
Edited: Sergej Gein
on 2 Sep 2022
Bruno Luong
on 2 Sep 2022
Edited: Bruno Luong
on 2 Sep 2022
Can you please try Torsen's formulation?
Bruno Luong
on 2 Sep 2022
"Deep explanation:" ..
I honestly don't understand from "Resulting Matrix A1 and B1 have partially the repeating items." onwards/
Sergej Gein
on 2 Sep 2022
Edited: Sergej Gein
on 2 Sep 2022
Bruno Luong
on 2 Sep 2022
"since A6B2 stores all possible combinations"
Be careful, the combinations are not necessary A in the first 6 letters and B the last 2 in case AorB = intersect(A1,B1) is a non-empty set.
My code pick m+n-i-j that is sorted elements to form SC. You would have to make all permutation of SC to get ALL combinations with 6 first letters from A and 2 first letters from B.
It is not clear from your previous question, since you use the word "subset", a set does not have order: "abc", "bac" "cba" ... all are the same
Categories
Find more on Get Started with Optimization Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!