Nested for loops and saving data in a vector
2 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Luna
on 31 Jan 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
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!