probably simple syntax error, I'm confused by bsxfun error

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

probably simple syntax error, I'm confused by bsxfun error

Tim Pierce
bsxfun according to the docs applies a function to each element of a vector 

x = [ 1 2 ; 3 4 ; 5 6 ]

bsxfun( @(a,b) (a*b), x(:,1), x(:,2) )

I'd expect

ans =
          2
         12
         30

it gives error

non conformant operators 3x1, 3x1 for operator *



_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: probably simple syntax error, I'm confused by bsxfun error

Andreas Weber-6
Hi Tim,

Am 01.09.2017 um 07:49 schrieb Tim Pierce:
> bsxfun according to the docs applies a function to each element of a vector
> |x = [ 1 2 ; 3 4 ; 5 6 ] bsxfun( @(a,b) (a*b), x(:,1), x(:,2) ) |
> I'd expect
>
> |ans = 2 12 30 |
>
> it gives error
> non conformant operators 3x1, 3x1 for operator *


From "help bsxfun":
...The function F must be capable of accepting two column-vector
arguments of equal length, or one column vector argument and a scalar...

so you are passing [1;3;5] and [2;4;6] to "(a*b)" but for a matrix
multiplication "*" the columns of a must match rows of b which isn't the
case. What you want is elementwise multiplication ".*"

I guess you want:

bsxfun( @(a,b) (a.*b), x(:,1), x(:,2) )
ans =

    2
   12
   30

But I can't see why you want to use bsxfun, in your case

prod(x, 2)
ans =

    2
   12
   30

would be much easier

HTH, Andy

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: probably simple syntax error, I'm confused by bsxfun error

Tim Pierce
Ahhhh. sorry. I've misunderstood.

Okay, well that makes bsxfun pretty redundant for me... or anyone... thx

I was looking for something like ArrayFun but row-wise

On Fri, Sep 1, 2017 at 7:22 AM, Andreas Weber <[hidden email]> wrote:
Hi Tim,

Am 01.09.2017 um 07:49 schrieb Tim Pierce:
> bsxfun according to the docs applies a function to each element of a vector
> |x = [ 1 2 ; 3 4 ; 5 6 ] bsxfun( @(a,b) (a*b), x(:,1), x(:,2) ) |
> I'd expect
>
> |ans = 2 12 30 |
>
> it gives error
> non conformant operators 3x1, 3x1 for operator *


From "help bsxfun":
...The function F must be capable of accepting two column-vector
arguments of equal length, or one column vector argument and a scalar...

so you are passing [1;3;5] and [2;4;6] to "(a*b)" but for a matrix
multiplication "*" the columns of a must match rows of b which isn't the
case. What you want is elementwise multiplication ".*"

I guess you want:

bsxfun( @(a,b) (a.*b), x(:,1), x(:,2) )
ans =

    2
   12
   30

But I can't see why you want to use bsxfun, in your case

prod(x, 2)
ans =

    2
   12
   30

would be much easier

HTH, Andy


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: probably simple syntax error, I'm confused by bsxfun error

Andreas Weber-6
Hi Tim, please use bottom or interleaved posting style on the mailinglist.

Am 01.09.2017 um 13:10 schrieb Tim Pierce:
> Okay, well that makes bsxfun pretty redundant for me... or anyone... thx

The important part on bsxfun is the "expansion", for example in

bsxfun (@plus, [1,2,3], [10;20])

without this you would have to repmat both argument to the matching size
of the other. Ans btw, there is automatic broadcasting since 3.8:

octave:3> [1,2,3]+[10;20]
ans =

   11   12   13
   21   22   23

HTH, Andy

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave