Can looping values be used for indexing?
Mostrar comentarios más antiguos
Hello, I have a variation of the following code running but I am looking to make the code a bit more efficient by looping instead of defining a unique variable each time.
LPCsale = zeros(3,19);
finalpop = [5000,1,5000,0,5000,1];
LLPmatrix = finalpop(:,2:2:end);
LPClim= 20000;
LPCcost = 100000;
for LPCloop = 1:3
LPCsale(LLPmatrix(:,LPCloop)) = round(((LPClim-(finalpop(LLPmatrix(:,LPCloop),1)))/LPClim)*LPCcost)-LPCcost;
disp(LPCsale)
end
In the working code there are LPCsale1, LPCsale2 and LPCsale3 variables. The LPCloop value is replaced by 1,2, and 3 instead.
Is it possible to use a looping value like this to perform indexing? I am receiving this error message curently:
"Index in position 1 is invalid. Array indices must be positive integers or logical values"
I will need to expand the number of LPCsale values further so I would like a more efficient way to do this than having blocks and blocks of similar code.
Appreciate any advice!
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!