Why do this in a loop when you can utilize MATLAB's ability to vectorize operations. This means you can simply perform all subtractions simultaneously, in which case all that is required is to specify the indices of the rows that you want to subtract: A = repmat((1:880).',1,4);
X = reshape(flipud(reshape(12:size(A,1),11,[])),[],1);
Y = 1:numel(X);
B = A(Y,:) - A(X,:);
The vector X contains the indices of all subtrahends, while Y is the indices of all minuends. The array B is the output, and it works for any number of columns of A.
Explanation: if you write out (in a column) all of the minuend indices, you will find that they are simply the positive integers 1,2,3,4,5,6,..... Likewise the subtrahend indices are every group of eleven integers, starting from twelve, reversed within every group. If you look at X and Y this is what you will see. And so we can generate these indices very easily, and perform all of the subtractions in one go.
PS: this is an interesting coincidence, two people trying to solve the same problem on the same weekend:
Best Answer