how to build logical relationships between elements of two vectors?
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
John D'Errico
2019-10-9
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.
回答(1 个)
Shivam Prasad
2019-10-14
编辑:Shivam Prasad
2019-10-14
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!