help-octave
[Top][All Lists]
Advanced

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

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


From: Tim Pierce
Subject: Re: probably simple syntax error, I'm confused by bsxfun error
Date: Fri, 1 Sep 2017 12:10:59 +0100

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 <address@hidden> 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


reply via email to

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