for loop to sum values

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

Read about sum
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
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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2022a

Asked:

on 15 Oct 2022

Answered:

on 15 Apr 2024

Community Treasure Hunt

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

Start Hunting!