Matlab bsxfun () code

What does it do?

u = [5 6];
s = [1 1];
data1    =[randn(10,1) -1*ones(10,1)];
data2    =[randn(10,1) ones(10,1)];
data     = [data1; data2];
deviance = bsxfun(@minus,data,u);  
deviance = bsxfun(@rdivide,deviance,s); 
deviance = deviance .^ 2; 
deviance = bsxfun(@plus,deviance,2*log(abs(s)));
[dummy,mini] = min(deviance,[],2);

Is there an equivalent way to do this without bsxfun?

+3
source share
2 answers

The BSXFUN function will perform the requested elementary operation (function descriptor argument) by replicating the dimensions of the two input arguments so that they match each other in size. You can avoid using BSXFUN in this case by replicating the variables uand by syourself using the REPMAT function to make them the same size as data. Then you can use standard elementary arithmetic operators :

u = repmat(u,size(data,1),1);  %# Replicate u so it becomes a 20-by-2 array
s = repmat(s,size(data,1),1);  %# Replicate s so it becomes a 20-by-2 array
deviance = ((data-u)./s).^2 + 2.*log(abs(s));  %# Shortened to one line
+3
source

bsxfun . , ( u) ( data). , , . bsxfun

u1=repmat(u,size(data,2),1);
deviance=data-u1;

.. .

+2

All Articles