I have a problem writing for loop and if statement
Show older comments
Hi, hope yall are staying safe. I am still learning Matlab. So what I am trying to do is change of Blood Volume ( c ) is equal to change of CSF volume ( f ) if they are not not equal I want to regenerate BV volume and CSF volume until they are equal to each other by using for loop and if statement. Can anyone help me? Thank you so much.
clear
clc
count = 0 ;
N = 10 ;
BV = zeros(N,1) ; %Blood Volume
for i=1:1000
BV_min = 100; %mL/min
BV_max = 150; %mL/min
BV_Volume = BV_min - rand(1,1)*(BV_min-BV_max);
if BV_Volume < 150 && BV_Volume >100
count = count+1 ;
BV(count) = BV_Volume
if count == 10;
break
end
else
continue
end
end
a = BV
b = BV
c = zeros(10,1)
for i = 2:10
c(i) = b(i-1) - a(i)
end
c = c(2:end)
count = 0 ;
N = 10 ;
CSF = zeros(N,1) ; % CSF volume
for i=1:1000
production_rate_min = 0.2; %mL/min
production_rate_max = 0.6; %mL/min
production_rate = production_rate_min - rand(1,1)*(production_rate_min- production_rate_max);
absorption_rate_min = 0.2; %mL/min
absorption_rate_max = 0.6; %mL/min
absorption_rate = absorption_rate_min - rand(1,1)*(absorption_rate_min- absorption_rate_max);
dVdt = production_rate - absorption_rate
CSF_Volume = (production_rate - absorption_rate)*60*24;
if CSF_Volume < 150 && CSF_Volume >100
count = count+1 ;
CSF(count) = CSF_Volume
if count == 10;
break
end
else
continue
end
end
d = CSF
e = CSF
f = zeros(10,1)
for i = 2:10
f(i) = e(i-1) - d(i)
end
f = f(2:end)
5 Comments
Image Analyst
on 17 Jul 2020
Edited: Image Analyst
on 17 Jul 2020
Possibly, but it would help if you added a lot more comments to your code so we can follow it. The units alone are not enough. And attach plots or something. Make it easy for us to help you. This link is useful. Your code runs, but all I see is a bazillion numbers with no meaning (to me). What do they all mean? Can anything be plotted? There are no errors, so is it working? Otherwise I see you say "want to regenerate BV volume and CSF volume until they are equal to each other by using for loop and if statement." so I'd just do
for k = 1 : length(BV) % Here's your for loop
if true % Here's your if statement
CSF(k) = BV(k); % Here's where they're made equal
end
end
Help us to understand what's going on without spending 15 minutes trying to figure out what's going on.
By the way, this:
d = CSF
e = CSF
f = zeros(10,1)
for i = 2:10
f(i) = e(i-1) - d(i)
end
f = f(2:end)
can be done more simply like
f = -diff(CSF);
Rachel Surridge
on 18 Jul 2020
Hello! It looks like there's some randomness involved in both your BV_Volume and CSF_Volume calculation. Are you trying to generate these values until you get them exactly equal by chance?
Sung Min Kwon
on 18 Jul 2020
Edited: Sung Min Kwon
on 18 Jul 2020
Rachel Surridge
on 18 Jul 2020
Thank you for the details!
I understand that c and f are intended to represent discrete values of dCSF_Volume/dt and dBV_Volume/dt between each sample of BV and CSF. If I understand correctly, you want to keep generating c and f until they are equal by chance (since they are based on BV_Volume and CSF_Volume, which are generated with an element of randomness).
You may want a while-loop rather than a for-loop to compare the values of c and f. You can place your entire code in a while loop so that it keeps running until c and f happen to be equal, like so:
flag = true;
while flag
% insert original code to calculate c and f
if c==f
flag = false;
end
end
If you wanted to generate c once, and keep generating f until it matched c, you could do a similar structure:
% insert original code to calculate BV and c
flag = true;
while flag
% insert original code to calculate CSF and f
if c==f
flag = false;
end
end
Note: this code compares every element of c to every element of f. c(1) must equal f(1), c(2) must equal f(2), and so on... for the code to terminate.
I'm hesitant to say that this is the answer you're looking for, since the likelihood that f == c is very low, and this while loop may (will almost certainly) run forever. If I've misunderstood your goal, please feel free to correct me!
Sung Min Kwon
on 20 Jul 2020
Edited: Sung Min Kwon
on 20 Jul 2020
Answers (0)
Categories
Find more on MATLAB Mobile in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!