MATLAB: Get the sum of consecutive columns of a matrix

sum of consecutive columns

I have a matrix of the dimensions of [1000 100]. Now I want to get the sum of 2 or 4 consecutive columns of the matix, therefore, finally I should have a matrix of the dimensions of [1000 50] or [1000 25], respectively. Can anyone propose a method for that without loops?

Best Answer

k = rand(1000, 100) ;
mat = reshape(k,1000,2,[]) ; % two columns
thesum = squeeze(sum(mat,2)) ;