how to build logical relationships between elements of two vectors?
9 views (last 30 days)
Show older comments
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
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.
Answers (1)
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
See Also
Categories
Find more on Multidimensional Arrays 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!