for loop to sum values

2 次查看(过去 30 天)
zhi cheng
zhi cheng 2022-10-15
I need to sum the value of
row column + row column + ... + row column
1 6,7,8 145 6,7,8 ... 6,7,8
2 6,7,8 146 6,7,8 ... 6,7,8
3 6,7,8 147 6,7,8 ... 6,7,8
... 6,7,8 ... 6,7,8 ... 6,7,8
144 6,7,8 288 6,7,8 52560 6,7,8
X=zeros(1,3);
x = 365
for i = 1:length(outDates(:,5))
avgspeeds = zeros(1,3);
for j = 1:3
sum1 = outDates(i,5+j) + outDates(i+n,5+j)
end
end
  2 个评论
zhi cheng
zhi cheng 2022-10-15
thank you for your reply, but I still dont get it from the info in helpcenter talk about how to sum the whole row/column
but I need to sum specific rows and columns eg: row1,row145,row289,row433.... there is a gap of 144rows between them and sum
row2,row146,row290,row434
row3,row147,row291,row435
so on until row 144
and the columns from 6-8

请先登录,再进行评论。

回答(3 个)

Jan
Jan 2022-10-15
编辑:Jan 2022-10-15
No loop needed:
X = reshape(outDates(:, 6:8), 144, [], 3); % Reshape to 3D array
Y = squeeze(sum(X, 2) / size(X, 2)); % Mean over 2nd dimension

Torsten
Torsten 2022-10-15
outDates = rand(52560,9);
sums = zeros(144,1);
for i=1:144
sums(i) = sum(outDates(i+(0:364)*144,6)) + sum(outDates(i+(0:364)*144,7)) + sum(outDates(i+(0:364)*144,8));
end
sums
sums = 144×1
547.5205 549.0608 553.0107 551.1408 534.0514 551.2122 553.6387 539.5439 546.9188 536.9660

Abdullah Emir
Abdullah Emir 2024-4-15
thank you

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by