How to iterate an if statement condition and store values in separate columns

1 vue (au cours des 30 derniers jours)
Joy Shen
Joy Shen le 1 Mar 2023
I'm trying to calculate Esim for different boundary conditions on Fsim. The first being if Fsim is less than n_ins, the second if Fsim is between n_ins and n_tot, and the third if Fsim is above n_tot.
However, I want to have three different values of n_tot. Is there a way to calculate this piecewise function, where one of the boundary conditions has three different values? I essentially would like Esim to have three columns of data for each n_tot(i).
This is what I have so far:
Qsim=[]; Qsim=Cd.*Asim.*sqrt(2*g*(Fsim-n_ins))*Dsim;
Vsim=[]; Vsim=Qsim.*Dsim; %ft^3
n_tot=[12 13 14];
for i=1:1:3
if (Fsim < n_ins)
Esim = 0;
elseif (Fsim >= n_ins & Fsim < n_tot(i))
elseif (Fsim>= n_tot(i))
Esim = int_L*int_W*int_H; % make sure this is here

Réponses (1)

DUY Nguyen
DUY Nguyen le 2 Mar 2023
You can try something like this! Esim will have three columns of data for each n_tot(i)
Qsim = Cd.*Asim.*sqrt(2*g*(Fsim-n_ins))*Dsim;
Vsim = Qsim.*Dsim; %ft^3
n_tot = [12 13 14];
Esim = zeros(length(Vsim), length(n_tot));
for i = 1:length(n_tot)
if (Fsim < n_ins)
Esim(:,i) = 0;
elseif (Fsim >= n_ins && Fsim(j) < n_tot(i))
Esim(:,i) = Vsim(j)/(int_L*int_W);
elseif (Fsim >= n_tot(i))
Esim(:,i) = int_L*int_W*int_H;


En savoir plus sur Logical dans Help Center et File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by