Nested for loops and saving data in a vector

조회 수: 6 (최근 30일)
xXTacitusXx
xXTacitusXx 2019년 1월 31일
댓글: Luna 2019년 2월 1일
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
  댓글 수: 2
xXTacitusXx
xXTacitusXx 2019년 1월 31일
Awesome, it worked, thank you very much!
Luna
Luna 2019년 2월 1일
Your welcome :)

댓글을 달려면 로그인하십시오.

추가 답변 (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