简体   繁体   中英

For loop inside a for loop?

I wish to write a code that would give me a [5x5] matrix containing values of "ec" for each step. But here I can only return its last value. Could you please help me?

Thanks for your interest

R = [0.13, 0.131, 0.132, 0.133, 0.134];
k = [1, 1.5, 2, 2.5, 3];
a = 3*60*6/1000;
for i=R
ec = 30 * (i*a + i*a*k/100)
endfor

It looks like you want something like

ec = zeros(5);
R = [0.13, 0.131, 0.132, 0.133, 0.134];
k = [1, 1.5, 2, 2.5, 3];
a = 3*60*6/1000;
for i_=1:length(R)
    for j_=1:length(k)
        ec(i_,j_) = 30 * (R(i_)*a + R(i_)*a*k(j_)/100);
    end
end

unless I'm mistaken about your question. This should return a 5x5 matrix ec .

A note about for loops: you should avoid using i as a counter, because this is predefined as equal to sqrt(-1), and if you reassign it there can be problems. Adding an underscore avoids this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM