help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Use of bsxfun


From: Bård Skaflestad
Subject: Re: Use of bsxfun
Date: Thu, 22 Sep 2011 14:18:02 +0200

On Thu, 2011-09-22 at 10:00 +0200, Juan Pablo Carbajal wrote:
> Not long ago JordiGH show me how to exploit the use of bsxfun. However
> I am having troubles using it
> Shouldn't the output of this call
> 
> > bsxfun (@ (x,y) x * y, [1 -1; 2 -2], [6 6; 3 3])
> 
> be [6 -6; 6 -6] and not [3 3; 6 6]? (The latter is what I get)

Your example demonstrates a slight deficiency in the BSXFUN interface.
To quote from the help text (in Octave 3.2.3, it may have changed in
later releases):

        Loadable Function:  bsxfun (F, A, B)
        Applies a binary function F element-wise to two matrix arguments
        A and B. ...

In this respect, your expectation ([6, -6; 6, -6]) is reasonable.
However, as the inputs 'A' and 'B' are (square) matrices, you are
effectively calling

        bsxfun(@mtimes, A, B)

which reduces to 'A * B' (i.e., ordinary matrix multiplication).  If you
want to apply element-by-element multiplication, you should be using
'array multiply', i.e., the @times function or spelled equivalently 

        @(x,y) x.*y

I can't really decide if calling BSXFUN with @MTIMES should be an error
or not.  There are good arguments for both sides.


Sincerely,
-- 
Bård Skaflestad <address@hidden>
SINTEF ICT, Applied Mathematics



reply via email to

[Prev in Thread] Current Thread [Next in Thread]