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

3 views (last 30 days)
Joy Shen on 1 Mar 2023
Answered: DUY Nguyen on 2 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))
Esim(:,i)=Vsim./(int_L*int_W);
elseif (Fsim>= n_tot(i))
Esim = int_L*int_W*int_H; % make sure this is here
end
end

DUY Nguyen on 2 Mar 2023
Hi,
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;
end
end

### Categories

Find more on Logical in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!