Nested for loops and saving data in a vector

2 visualizaciones (últimos 30 días)
xXTacitusXx
xXTacitusXx el 31 de En. de 2019
Comentada: Luna el 1 de Feb. de 2019
Hello there,
I have a function that depends on two variables and I want to iterate through both variables and get the end result as a vector.
what I achieved so far is iterating through the variable r=1:7 (the seven entries in the vector "alpha") and getting a vector lambda(r) with seven entries.
That looks like this:
alpha=[-0.17445,0.09672,0.36789,0.63906,0.91023,1.1814,1.45257];
for r=1:7
lambda(r)=10^(alpha(r)-log(L));
end
For testing purposes I just put L=10 above the code and it works, but I need to do this for L=1:100 so that I have at the end 100 sets of those seven values and I don't know how to save the seven r variables for each L after each step, let alone how to tell matlab to increase L after seven iterations. I could do the for loop 100 times and change the value manually, but that is obviously not suitable.
I tried nested for loops, but I can't get it to work.
Help would be greatly appreciated.

Respuesta aceptada

Luna
Luna el 31 de En. de 2019
Hi,
First start with preallocation. lambda will be your 100x7 matrix, each row is a set of 7 calculation.
alpha=[-0.17445,0.09672,0.36789,0.63906,0.91023,1.1814,1.45257];
L = 1:100;
lambda = nan(numel(L),numel(alpha)); % preallocation
for k = 1:numel(L)
for r = 1:numel(alpha)
lambda(k,r) = 10^(alpha(r) - log(L(k)));
end
end
  2 comentarios
xXTacitusXx
xXTacitusXx el 31 de En. de 2019
Awesome, it worked, thank you very much!
Luna
Luna el 1 de Feb. de 2019
Your welcome :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by