Matlab's bsxfun() code

前端 未结 2 1587
既然无缘
既然无缘 2021-01-21 08:19

What does this 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(         


        
2条回答
  •  自闭症患者
    2021-01-21 08:51

    The function BSXFUN will perform the requested element-wise operation (function handle argument) by replicating dimensions of the two input arguments so that they match each other in size. You can avoid the use of BSXFUN in this case by replicating the variables u and s yourself using the function REPMAT to make them each the same size as data. Then you can use the standard element-wise 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
    

提交回复
热议问题