Shrink for loop with if conditions
Show older comments
Dear all,
Currently, I am writing some code that will calculate the CO2 capture of trees over time and it is used by a municipality to make decision upon. Basically, they only have to fill in an Excel and will receive their end values and graph from me. I run into one little problem:
I have to write many if conditions to make sure that all reasonable situations can be calculated. I need to find a way to write this shorter. I need this for loop to delay the carbon to be emitted into the atmosphere, based on the use of the endproduct.
filename = 'INPUTOUTPUT.xlsx';
sheet = 'INPUT';
Age1 = xlsread(filename, sheet, 'E7');
Rotation = xlsread(filename, sheet, 'E8');
Rotations1 = Age1/Rotation;
Rotations = round(Rotations1);
Age = 0:1:(Age1)-1;
Standage = 0:1:(Rotation)-1;
R = xlsread(filename, sheet, 'E32');
% Normal discount factor
VF = zeros(1,numel(Age));
for i = 1:numel(Age)
VF(i) = 1/((1+R)^(Age(i)));
end
% CO2 discount factor
delay = xlsread(filename, sheet, 'E30');
VF_CO2 = zeros(1,numel(Standage));
for i = 1:numel(Age)
if (Age(i) == Rotation)
VF_CO2(i) = 1/((1+R)^(Age(i)+delay));
elseif (Age(i) == 2*Rotation)
VF_CO2(i) = 1/((1+R)^(Age(i)+delay));
elseif (Age(i) == 3*Rotation)
VF_CO2(i) = 1/((1+R)^(Age(i)+delay));
elseif (Age(i) == 4*Rotation)
VF_CO2(i) = 1/((1+R)^(Age(i)+delay));
elseif (Age(i) == 5*Rotation)
VF_CO2(i) = 1/((1+R)^(Age(i)+delay));
elseif (Age(i) == 6*Rotation)
VF_CO2(i) = 1/((1+R)^(Age(i)+delay));
elseif (Age(i) == 7*Rotation)
VF_CO2(i) = 1/((1+R)^(Age(i)+delay));
elseif (Age(i) == 8*Rotation)
VF_CO2(i) = 1/((1+R)^(Age(i)+delay));
elseif (Age(i) == 9*Rotation)
VF_CO2(i) = 1/((1+R)^(Age(i)+delay));
elseif (Age(i) == 10*Rotation)
VF_CO2(i) = 1/((1+R)^(Age(i)+delay));
elseif (Age(i) == 11*Rotation)
VF_CO2(i) = 1/((1+R)^(Age(i)+delay));
elseif (Age(i) == 12*Rotation)
VF_CO2(i) = 1/((1+R)^(Age(i)+delay));
elseif (Age(i) == 13*Rotation)
VF_CO2(i) = 1/((1+R)^(Age(i)+delay));
elseif (Age(i) == 14*Rotation)
VF_CO2(i) = 1/((1+R)^(Age(i)+delay));
elseif (Age(i) == 15*Rotation)
VF_CO2(i) = 1/((1+R)^(Age(i)+delay));
else
VF_CO2(i) = VF(i);
end
end
As you can see, I need 15 'if/elseif' conditions. The number of rotations are all multiplied by an integer. How can I write this shorter and inclusive, because right now only 15 rotations are possible?
Thanks in advance,
Barend
1 Comment
David Fletcher
on 27 May 2021
Maybe I'm missing something fundamental (entirely possible) but what purpose do the if statements serve? As far as I can see all the expressions in the cases are the same and there is no variable reassignment, so don't they all evaluate to the same thing whichever case is active. The only exception is the else clause, but surely you only need one if statement to select between 1-15* (presumably Age(i)/Rotation) and everything else.
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!