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 comentario

David Fletcher
David Fletcher el 27 de Mayo de 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.

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 27 de Mayo de 2021
Replace
% Normal discount factor
VF = zeros(1,numel(Age));
for i = 1:numel(Age)
VF(i) = 1/((1+R)^(Age(i)));
end
by
VF = 1 ./ ((1 + R) .^ Age);
The bunch of if-conditions can be omited:
VF_CO2 = VF;
m = ismember(Age / Rotation, 1:15);
VF_CO2(m) = 1 ./ ((1 + R) .^ (Age(m) + delay));

1 comentario

Barend Kok
Barend Kok el 28 de Mayo de 2021
Thanks so much! Guess you showed me a way to write a lot of stuff in my code way shorter.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Entering Commands en Centro de ayuda y File Exchange.

Productos

Versión

R2021a

Preguntada:

el 27 de Mayo de 2021

Comentada:

el 28 de Mayo de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by