How to avoid that loop
1 次查看(过去 30 天)
显示 更早的评论
k(1) = 1;
for i=2:size(k)
k(i) = k(i-1)*v(i)
end
v(i) is a scalar and it's different on every iteration How could I do that without using a loop?
2 个评论
the cyclist
2016-5-3
编辑:the cyclist
2016-5-3
As written, this loop will never be executed, because size(k) is 1, and
for i = 2:1
<stuff>
end
will have zero iterations.
Some coding mistake? Maybe you meant length(v)?
采纳的回答
the cyclist
2016-5-3
If my speculation about what you meant it correct, then
k = cumprod(v)/v(1)
2 个评论
Steven Lord
2016-5-3
No, we understand you. Another approach that doesn't involve division:
v = randperm(8) % Sample data for demonstration purposes
k = cumprod([1 v(2:end)])
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!