简体   繁体   English

for循环内的for循环?

[英]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. 我希望编写一个代码,使我得到一个[5x5]矩阵,其中每个步骤的值都为“ ec”。 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 . 这应该返回5x5矩阵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. 关于for循环的注意事项:应避免将i用作计数器,因为该计数器的预定义值等于sqrt(-1),如果重新分配它,可能会出现问题。 Adding an underscore avoids this problem. 添加下划线可以避免此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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