[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Use of bsxfun
From: |
Jordi Gutiérrez Hermoso |
Subject: |
Re: Use of bsxfun |
Date: |
Thu, 22 Sep 2011 08:57:57 -0500 |
On 22 September 2011 07:30, Juan Pablo Carbajal <address@hidden> wrote:
> On Thu, Sep 22, 2011 at 2:18 PM, Bård Skaflestad
> <address@hidden> wrote:
>> 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.
>
> Thank you very much Bárd,
>
> I think it would be a good idea to put this example in the help text.
> It is a built-in function so I cannot do it. Who should I contact?
Me. What exactly do you want the example to look like?
In the current development branch of Octave, your example is simply "x
.* y". I've made bsxfun automatic in the development branch. It should
be a frequent and natural vectorisation technique, and I think
bsxfun's obscure syntax is preventing people from using it more
easily.
- Jordi G. H.