average every nth column in matrix with n getting larger in matlab

You can achieve this using dynamic programming in Matlab.

Here is an example code:

main.m
%Generate random matrix A with dimensions n x m
n = 10; %number of rows
m = 20; %initial number of columns
A = rand(n,m);

%Define the value of increment k
k = 2;

%Initialize the new matrix B 
B = zeros(n,fix(m/k));

%Iterate through the columns, averaging every k columns
for i = 1:fix(m/k)
    %Define column range to average
    col_range = (i-1)*k+1:i*k;
    %Average the values in the column range
    B(:,i) = mean(A(:,col_range),2);
end
445 chars
19 lines

This script generates a random matrix A with dimensions n by m. It then defines an increment k for the number of columns to average per iteration. A new matrix B is initialized with dimensions n by fix(m/k). Finally, the script iterates through every k columns in A, averages the values in the column range, and stores the result in B.

gistlibby LogSnag