Borrar filtros
Borrar filtros

Using for loops in referencing

1 visualización (últimos 30 días)
Lui
Lui el 18 de Jul. de 2019
Comentada: Lui el 20 de Jul. de 2019
Hello everyone
I would like to create new datasets separately through a for loop.
for n=0:5:40
netload'n'=Y-n.*Koutput
end
% the results should give me nine outputs which would be somethig like
netload0=Y-0.*Koutput
netload5=Y-5.*Koutput
"" + ...
""
netload40=Y-40.*Koutput
How can I execute that using a for loop?
Thank you.
  1 comentario
Guillaume
Guillaume el 18 de Jul. de 2019
You should never create numbered, or sequentially named, variables. Embedding any form of indexing in the variable name is always a bad design. See Tutorial: Why Variables Should Not Be Named Dynamically for more details.

Iniciar sesión para comentar.

Respuesta aceptada

Guillaume
Guillaume el 18 de Jul. de 2019
Editada: Guillaume el 19 de Jul. de 2019
You don't even need a loop to perform your calculation:
n = permute(0:5:40, [1, 3, 2]); %create a vector 0:5:40 in the 3rd dimension
netload = Y - n .* Koutput; %create a 8760 x 42 x numel(n) matrix
netload(:, :, 1) corresponds to n = 0, netload(:, :, 2) corresponds to n = 5, etc.
  5 comentarios
Guillaume
Guillaume el 19 de Jul. de 2019
Thanks, Andrei. Fixed now.
Lui
Lui el 20 de Jul. de 2019
Thank you so much. This works

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by