Nested for loops and saving data in a vector

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 日

1 投票

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 件のコメント

xXTacitusXx
xXTacitusXx 2019 年 1 月 31 日
Awesome, it worked, thank you very much!
Luna
Luna 2019 年 2 月 1 日
Your welcome :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2019 年 1 月 31 日

コメント済み:

2019 年 2 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by