From: Piotr Wydrych on 25 Apr 2010 10:41 Hi all! The documentation of the bsxfun function states that ``If an M-file function is specified, it must be able to accept either two column vectors of the same size, or one column vector and one scalar, and return as output a column vector of the size as the input values.'' I have recently found that when I call bsxfun(@fun, A, B), where A is a scalar and B is a row vector, the function fun is called once and bsxfun passes exactly A and B to fun. I realize that it is faster to call the function once instead of length(B) times, but it does not conform with the documentation, since fun does not have to accept row vectors... Please look at the following example: function z = bsxfuntest(x, y) disp([size(x), size(y)]) z = x + y; end %% simplest case (scalar, scalar) % should display 1 1 1 1 bsxfun(@bsxfuntest, 1, 1); % OK, bsxfuntest is called once, what is obvious %% scalar and rvector % should display 1 1 1 1<cr>1 1 1 1 bsxfun(@bsxfuntest, 1, 1:2); % WRONG, bsxfuntest is called once, with scalar and rvector! %% cvector and scalar % should display 3 1 1 1 bsxfun(@bsxfuntest, (1:3)', 1); % OK, bsxfuntest is called once, with cvector and scalar %% cvector and rvector % should display 3 1 1 1<cr>3 1 1 1 bsxfun(@bsxfuntest, (1:3)', 1:2); % OK, bsxfuntest is called twice, with cvector and scalar each time Concluding: is it a bug or an undocumented feature? My software is 7.9.0.529 (R2009b) and arch is GLNXA64. Best regards, Piotr Wydrych
|
Pages: 1 Prev: bar plot, why some bars are of diiferent width Next: Curve Fitting Multiple Data Lines |