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

4 messages
Open this post in threaded view
|

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

 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 errornon conformant operators 3x1, 3x1 for operator * _______________________________________________ Help-octave mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-octave
Open this post in threaded view
|

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

 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