Nested for loops and saving data in a vector

2 次查看(过去 30 天)
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.

采纳的回答

Luna
Luna 2019-1-31
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by