I have a list of 120 elements in a column, and I need to sum each 12 elements and then sum together the next 12 so from the 120 values I will have 12 different sum values.
1 次查看(过去 30 天)
显示 更早的评论
I just need a for loop to get the sum of each 12 elements in the column for 10 consequative times.
0 个评论
回答(2 个)
hari
2020-2-10
sum = 0;
temp = 0;
totalSum = [];
for i = 1:length(c)
sum = sum+c(i);
temp = temp+1;
if (temp == 12)
ttalSum = [totalSum sum];
sum = 0;
temp = 0;
end
end
2 个评论
Walter Roberson
2020-2-10
We firmly recommend that you never use sum as the name of a variable, as it is very common to also need to use sum() as a function call.
This is especially important when you are using scripts, because that variable named sum is going to be left in the workspace and you are going to get very confused when you have forgotten it and try to use sum() function.
KSSV
2020-2-10
Type error....
ttalSum = [totalSum sum];
shoule be replaced to :
totalSum = [totalSum sum];
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numeric Types 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!