for loop to sum values

2 views (last 30 days)
zhi cheng
zhi cheng on 15 Oct 2022
Answered: Abdullah Emir on 15 Apr 2024
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 Comments
KSSV
KSSV on 15 Oct 2022
Read about sum
zhi cheng
zhi cheng on 15 Oct 2022
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

Sign in to comment.

Answers (3)

Jan
Jan on 15 Oct 2022
Edited: Jan on 15 Oct 2022
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 on 15 Oct 2022
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 on 15 Apr 2024
thank you

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!