Are there two conditions at once when using the if condition?

조회 수: 1 (최근 30일)
채원 이
채원 이 2022년 1월 19일
답변: Prince Kumar 2022년 1월 25일
Hello. I have a question and am posting.
I solved it as below, but the value I want is not found, so I ask. Q_1 is entered only when tact is 1 and k is 1, and I am trying to calculate the rest by putting the rest in the expression.
So, when tact is 1 and k is 1, can't we just put the value of Q_1?? For the rest, I try to use the formula below.​
Q_1 = 30;
Q_D = 11.3;
Q_E = 8.3;
Q_R = 7.5;
Q_F = 4.5;
connection_D = [1,0,0,0; 0,1,0,0; 0,0,1,0; 0,0,0,1];
connection_E = [0,1,0,0; 0,0,1,0; 0,0,0,1; 1,0,0,0];
connection_F = [0,0,1,0; 0,0,0,1; 1,0,0,0; 0,1,0,0];
connection_R = [0,0,0,1; 1,0,0,0; 0,1,0,0; 0,0,1,0];
for tact = 1:4
for k=1:4
if k==1
Q_column(1)=Q_1
else
Q_column(k) = Q_column(k-1) + Q_D*connection_D(tact,k) - Q_E*connection_E(tact,k) + Q_F*connection_F(tact,k) - Q_R*connection_R(tact,k)
end
end
end

답변 (1개)

Prince Kumar
Prince Kumar 2022년 1월 25일
Hi,
You can use logical operator & to achieve this.
Q_1 = 30;
Q_D = 11.3;
Q_E = 8.3;
Q_R = 7.5;
Q_F = 4.5;
connection_D = [1,0,0,0; 0,1,0,0; 0,0,1,0; 0,0,0,1];
connection_E = [0,1,0,0; 0,0,1,0; 0,0,0,1; 1,0,0,0];
connection_F = [0,0,1,0; 0,0,0,1; 1,0,0,0; 0,1,0,0];
connection_R = [0,0,0,1; 1,0,0,0; 0,1,0,0; 0,0,1,0];
for tact = 1:4
for k=1:4
if k==1 & tact == 1
Q_column(1)=Q_1
else
Q_column(k) = Q_column(k-1) + Q_D*connection_D(tact,k) - Q_E*connection_E(tact,k) + Q_F*connection_F(tact,k) - Q_R*connection_R(tact,k)
end
end
end
You can use any combination of operators as per your need.
Hope this helps.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by