4 views (last 30 days)

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!

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)

Opportunities for recent engineering grads.

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

Start Hunting!
## 3 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/484376-how-to-build-logical-relationships-between-elements-of-two-vectors#comment_754368

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/484376-how-to-build-logical-relationships-between-elements-of-two-vectors#comment_754368

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/484376-how-to-build-logical-relationships-between-elements-of-two-vectors#comment_754376

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/484376-how-to-build-logical-relationships-between-elements-of-two-vectors#comment_754376

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/484376-how-to-build-logical-relationships-between-elements-of-two-vectors#comment_754390

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/484376-how-to-build-logical-relationships-between-elements-of-two-vectors#comment_754390

Sign in to comment.