我想做一个for循环,其中数组中的值相加,因此第一个值仍然相同,第二个值变为数组中第一个和第二个值的总和,第三个变为总和数组中的第1,第2和第3个值等。
den_p = 0;
for lp = 1 : bars-1
den_p = den_p + lCubed_p(1,lp)./In_p(1,lp)
den_w = lCubed_w./In_w;
end
生成的值是正确的,但我想将它们存储在长度为lp的数组中,我尝试过:
den_p = 0;
for lp = 1 : bars-1
den_p(1,lp) = den_p(1,lp) + lCubed_p(1,lp)./In_p(1,lp)
den_w = lCubed_w./In_w;
end
但该功能本身不能自行调用。
答案 0 :(得分:0)
如果我理解正确的话,Vectorise应该在这里做得很好,速度和解决你的问题。
%example for den_p only as no explanation given for requirements for den_w
lp = 1:bars-1;
den_p = zeros(1,bars-1); %generate an empty array in the right size
%kept vector operation and cumulative sum separated for ease of understanding
%if unfamiliar with vectorising
den_p(1,lp) = lCubed_p(1,lp)./In_p(1,lp); %vectorised assignment to array
den_p(1,lp) = cumsum(den_p(1,lp),2); %Cumulative sum along 2nd dimension
有关详细信息,请参阅cumsum()
希望这有帮助