[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: probably simple syntax error, I'm confused by bsxfun error
From: |
Andreas Weber |
Subject: |
Re: probably simple syntax error, I'm confused by bsxfun error |
Date: |
Fri, 1 Sep 2017 08:22:38 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 |
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