如何替代以下for循环?。
Show older comments
代码表达的意思是
截取a数组的3段:[2 3 4],[3 4 5],[4 5 6]. 然后3段数组相加
########################################
clc;
a = [2 3 4 5 6 7];
b = zeros(1,3);
for i = 1:3
b = b + a(i:i+2);
end
结果:
b =
9 12 15
###########################################
我用一下方式尝试替代for循环
a = [2 3 4 5 6 7];
i = 1:3;
b = zeros(1,3);
b = b + a(i:i+2);
结果:
b =
2 3 4
############################################
我的核心问题是,如何将for i = 1:3这样的for循环用非循环替代从而节省运算时间
Accepted Answer
More Answers (0)
Categories
Find more on 循环及条件语句 in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!