MATLAB Answers

how to build logical relationships between elements of two vectors?

2 views (last 30 days)
BOWEN LI
BOWEN LI on 9 Oct 2019
Edited: Shivam Prasad on 14 Oct 2019
Hi everyone. I have a question that how can i correlate the element in two matrices. For example that suppose i have a matrix A=[a1 a2 a3 a4] and matrix B=[b1 b2], both a1 throught a4 and b1 through b4 are binary decions variables meaning that they can either be 0 or 1.
My question is that I want to put a correlation (or like a constraint) between matrix A and B that b1 turns to 1 when and only when a1 and a2 turn to 1, and b2 turns to 1 when and only when a3 and a4 turns to 1.
thank you!

  3 Comments

Adam
Adam on 9 Oct 2019
I'm not aware that you can do this on raw arrays, but you could write a simple function to calculate b from a and call that instead of trying to keep a b-vector permanently updated. Or you could wrap them together in a class and either automatically change b when a changes or make it into a dependent property (which is essentially the same as the first option of making it into a function, except it becomes an implementation detail rather than a visible change code-wise.
BOWEN LI
BOWEN LI on 9 Oct 2019
Hi Adam thanks so much for your answer. I tried to put a and b together either as a function or dependent property, but in my case, both a and b are actually my decision variables of my integer optimization problem. I have like many 0-1 type decision varibles (in this case, 6 decision varibles), and A has the first 4 of them (a1-a4) and B has the last 2 (b1-b4). So I am trying to let b be automatically updated if a1 and a2 are simutaneously turn to 1 and for b2 as well, as you mentioned b and a have a dependent property. So I stucked here that this kind of logical relationship i have no exact idea of how to interpret it into codes. Thank you!
John D'Errico
John D'Errico on 9 Oct 2019
You cannot do this with two standard arrays or vectors, that is, have them automatically linked in some way that when one changes, the other does too.
You cam write a function that computes b from a. But you would need to execute that function whenever you were interested in the value of b.

Sign in to comment.

Answers (1)

Shivam Prasad
Shivam Prasad on 14 Oct 2019
Edited: Shivam Prasad on 14 Oct 2019
Hi BOWEN,
Code for modifying the values of B according to values in A:-
A = [1 0 1 0;1 1 0 0;1 1 1 1;1 1 0 1;0 1 1 1]; % define some nX4 array.
B = [1 1;0 1;1 0;0 0;1 0]; % define some nX2 array.
if(size(A,1)~=size(B,1))
error('Dimensions incompatible') % the number of rows must match.
end
for i=1:size(A,1)
if(A(i,1)==1 && A(i,2)==1) % if both a1 and a2 are 1, turn b1 to 1
B(i,1)=1;
end
if(A(i,3)==1 && A(i,4)==1) % if both a3 and a4 are 1, turn b2 to 1
B(i,2)=1;
end
end
disp(B)
Code for implementing the above constraint:-
A=[1 0 1 0;1 1 0 0;1 1 1 1;1 1 0 1;0 1 1 1]; % define some nX4 array.
B=[1 1;0 1;1 0;0 0;1 0]; % define some nX2 array.
if(size(A,1)~=size(B,1))
error('Dimensions incompatible') % the number of rows must match.
end
for i=1:size(A,1)
if(A(i,1)==1 && A(i,2)==1) && B(i,1)==0
error('Constraint a1,a2,b1 failed!') % if both a1 and a2 are 1, b1 should be 1
end
if(A(i,3)==1 && A(i,4)==1) && B(i,2)==0
error('Constraint a3,a4,b2 failed!') % if both a3 and a4 are 1, b2 should be 1
end
end
disp(B)

  0 Comments

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!