1
$\begingroup$

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?

$\endgroup$
2
$\begingroup$

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.

$\endgroup$
2
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.