Moving mean in matrix using first column

I have a matrix:
a = [1 3 5 2 4 6 7 8 10;
4 3 5 2 8 6 7 8 7;
5 8 9 7 2 3 6 1 5]
I would like to calculate the mean of the first column and the next four columns:
[1;4;5] and [3;3;8]
[1;4;5] and [5;5;9]
[1;4;5] and [2;2;7]
[1;4;5] and [4;8;2]
Then I would like to do the same starting for a(:,2) where it also takes the mean with the previous column:
[3;3;8] and [1;4;5]
[3;3;8] and [5;5;9]
[3;3;8] and [2;2;7]
[3;3;8] and [4;8;2]
until I have done this for all the columns. For the last column I would like it to start with the first column of the matrix again after taking the mean with the previous column so:
[10;7;5] and [8;8;1]
[10;7;5] and [1;4;5]
[10;7;5] and [3;3;8]
[10;7;5] and [5;5;9]
Is there a way in which I could use movmean() to achieve this? Or is there a better way?
Thanks

 Accepted Answer

a = [1 3 5 2 4 6 7 8 10;
4 3 5 2 8 6 7 8 7;
5 8 9 7 2 3 6 1 5];
res=@(z) reshape(z,3,1,[]);
A=arrayfun( @(i) res( circshift(a,[0,-i]) ) , [-1,1,2,3],'uni',0);
A=cell2mat(A);
output=(res(a)+A)/2;
output(:,:,1)= a(:,1)/2+a(:,2:5)/2
output =
output(:,:,1) = 2.0000 3.0000 1.5000 2.5000 3.5000 4.5000 3.0000 6.0000 6.5000 7.0000 6.0000 3.5000 output(:,:,2) = 2.0000 4.0000 2.5000 3.5000 3.5000 4.0000 2.5000 5.5000 6.5000 8.5000 7.5000 5.0000 output(:,:,3) = 4.0000 3.5000 4.5000 5.5000 4.0000 3.5000 6.5000 5.5000 8.5000 8.0000 5.5000 6.0000 output(:,:,4) = 3.5000 3.0000 4.0000 4.5000 3.5000 5.0000 4.0000 4.5000 8.0000 4.5000 5.0000 6.5000 output(:,:,5) = 3.0000 5.0000 5.5000 6.0000 5.0000 7.0000 7.5000 8.0000 4.5000 2.5000 4.0000 1.5000 output(:,:,6) = 5.0000 6.5000 7.0000 8.0000 7.0000 6.5000 7.0000 6.5000 2.5000 4.5000 2.0000 4.0000 output(:,:,7) = 6.5000 7.5000 8.5000 4.0000 6.5000 7.5000 7.0000 5.5000 4.5000 3.5000 5.5000 5.5000 output(:,:,8) = 7.5000 9.0000 4.5000 5.5000 7.5000 7.5000 6.0000 5.5000 3.5000 3.0000 3.0000 4.5000 output(:,:,9) = 9.0000 5.5000 6.5000 7.5000 7.5000 5.5000 5.0000 6.0000 3.0000 5.0000 6.5000 7.0000

More Answers (0)

Asked:

on 24 Mar 2022

Edited:

on 24 Mar 2022

Community Treasure Hunt

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

Start Hunting!