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

1 visualizzazione (ultimi 30 giorni)
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

Risposte (1)

Prince Kumar
Prince Kumar il 25 Gen 2022
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.

Categorie

Scopri di più su Operating on Diagonal Matrices in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by