To calculate the mean of the first N elements of an array, you can use
However, you may import a .mat file which I suppose will give you a matrix in which each row or column is one array. In that case, just perform
mean_mat = mean(Y(:,1:N),2);
mean_mat = mean(Y(1:N,:),1);
and you will get a matrix containing the mean of first N elements of each array.
This should be a more efficient way to compute the mean than to use a for loop, but if using a for-loop is necessary, feel free to let me know.