# Maximising each element in a matlab array

Is it possible to construct an array A, without looping, where A(i,j) = max(B(i),C(j)) and B and C are column vectors?

Try using bsxfun if your version has it.

A = bsxfun(@max, B, C.')


As per their documentation, bsxfun expands the dimensions of the argument matrices on-the-fly, so does not use as much memory as repmat.

If the matrices are not too big, repmat could work:

B = [2 3];
C = [0 1 2 3];
% Vectorize the vectors for a simplication
B = B(:);
C = C(:);

A = max(repmat(B,1,length(C)),repmat(C',length(B),1));


For those interested, Comparing BSXFUN and REPMAT and Matlab - bsxfun no longer faster than repmat? address its relative efficiency with respect to repmat. Apparently, bsxfun used to be much faster, sometime ago. The difference seems shallower with the recent Matlab versions.