Search code examples
matlabbsxfun

bsxfun: Dimensions of matrices being concatenated are not consistent


Any one knows where the error is? Many thanks!

beta=randn(50,1);
bsxfun(@(x1,x2) max([x1 x2 x1+x2]), beta, beta')

error message:

Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Error in @(x1,x2)max([x1,x2,x1+x2])


Solution

  • Here's what I got (using two max in bsxfun)

    beta = randn(50,1); 
    res = bsxfun(@(x,y) max( x, max(y, x+y) ), beta, beta');
    

    Verifying using repmat

    tmp = max( cat(3, repmat(beta,[1 50]), repmat(beta',[50 1]), ...
                      repmat(beta,[1 50])+ repmat(beta',[50 1]) ), [], 3);
    isequal( tmp, res )