I consider the general case (the matrix is not necessarily square). Let be
m = 4; %// number of rows n = 3; %// number of columns
There are quite a few approaches:
Using ndgrid :
[ii jj] = ndgrid(1:m,1:n); result = (ii.*jj).*(ii~=jj);
Using bsxfun :
result = bsxfun(@times, (1:m).',1:n) .* bsxfun(@ne, (1:m).',1:n);
Using repmat and cumsum :
result = cumsum(repmat(1:n,m,1)); result(1:m+1:m^2) = 0;
Using matrix multiplication (added by @ GastónBengolea):
result = (1:m).'*(1:n).*~eye(m,n);
source share