MATLAB: How to make a loop that performs the same operation on a large matrix

for looplarge matrixmeanvariance

I know this will have been asked before, but I can't seem to work out the answer after quite a lot of hours of searching; if someone could help I'd be really greatful
I have a matrix that is 64 x 149 (64 rows, 149 columns)
I would like to work out the mean and variance for groups of three rows, going down by one each time
So, rows 1, 2 and 3, then rows 2, 3 and 4, rows 3, 4 and 5 etc…
I'm sure there must be some way of doing this other than manually imputting it for every single calculation – perhaps using a for loop? But as to actually make this work I'm somewhat lost.
Apologies for my ineptitude; if anyone could help I would be so greatful!!

Best Answer

  • for k=1:62
    m(k)=mean(yourMatrix(k:k+2,:),'all');
    v(k)=var(yourMatrix(k:k+2,:),1,'all');
    end