# How can I average values from 3 consecutive rows of a matrix in one designated column repeatedly over the whole matrix?

4 views (last 30 days)
Andri Graf on 28 Apr 2022
Commented: Andri Graf on 28 Apr 2022
This is what an example of the original matrix A looks like.
A = 6x6
1 3 4 3 4 5
2 4 5 7 7 4
7 9 5 6 4 3
4 9 4 5 3 2
9 3 2 2 1 1
4 5 6 1 1 2
Now i would like to create a column array with two values. The first value woul be the average of A(1,2),A(2,2),A(3,2) and the second value would be when you calculate the average of A(4,2),A(5,2),A(6,2).
This would then form the column array B
B = 2x1
5.33
5.66
Now comes the important part. My matrix has over 200'000 rows and i want to do this repeatedly over all rows of the matrix in one column. Always calculate the mean value of the numbers at the same column position in 3 consecutive rows and then take the mean of the next 3 and so on... And then create a new column array with all the mean values (the length of the new array would be a third of the original matrix). I don't know the absolute length of the matrix since it can vary due to different measurements so it should be some kind of loop that does not take forever to calculate since there are so many rows.
Thank you already for your time.
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Benjamin Thompson on 28 Apr 2022
If your matrix can have a number of rows that is a multiple of three, and you should know its size at the time you need to perform these calculations, then you can use reshape and then rely on the behavior of the mean function when operating on matrices like this:
>> A = rand(5,6)
A =
0.8147 0.0975 0.1576 0.1419 0.6557 0.7577
0.9058 0.2785 0.9706 0.4218 0.0357 0.7431
0.1270 0.5469 0.9572 0.9157 0.8491 0.3922
0.9134 0.9575 0.4854 0.7922 0.9340 0.6555
0.6324 0.9649 0.8003 0.9595 0.6787 0.1712
>> A = rand(15,6)
A =
0.7060 0.4898 0.7513 0.3500 0.0759 0.6020
0.0318 0.4456 0.2551 0.1966 0.0540 0.2630
0.2769 0.6463 0.5060 0.2511 0.5308 0.6541
0.0462 0.7094 0.6991 0.6160 0.7792 0.6892
0.0971 0.7547 0.8909 0.4733 0.9340 0.7482
0.8235 0.2760 0.9593 0.3517 0.1299 0.4505
0.6948 0.6797 0.5472 0.8308 0.5688 0.0838
0.3171 0.6551 0.1386 0.5853 0.4694 0.2290
0.9502 0.1626 0.1493 0.5497 0.0119 0.9133
0.0344 0.1190 0.2575 0.9172 0.3371 0.1524
0.4387 0.4984 0.8407 0.2858 0.1622 0.8258
0.3816 0.9597 0.2543 0.7572 0.7943 0.5383
0.7655 0.3404 0.8143 0.7537 0.3112 0.9961
0.7952 0.5853 0.2435 0.3804 0.5285 0.0782
0.1869 0.2238 0.9293 0.5678 0.1656 0.4427
>> Acolumn2 = A(:,2)
Acolumn2 =
0.4898
0.4456
0.6463
0.7094
0.7547
0.2760
0.6797
0.6551
0.1626
0.1190
0.4984
0.9597
0.3404
0.5853
0.2238
Areshape = reshape(Acolumn2,3,5)
Areshape =
0.4898 0.7094 0.6797 0.1190 0.3404
0.4456 0.7547 0.6551 0.4984 0.5853
0.6463 0.2760 0.1626 0.9597 0.2238
>> mean(Areshape)
ans =
0.5272 0.5800 0.4991 0.5257 0.3832
##### 2 CommentsShowHide 1 older comment
Andri Graf on 28 Apr 2022
Thank you very much Benjamin, this is exactly what I was looking for! Very elegant solution.

Sign in to comment.

R2020b

### Community Treasure Hunt

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

Start Hunting!