Matlab's bsxfun() code

前端 未结 2 1571
既然无缘
既然无缘 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:46

    bsxfun does binary operations element wise. It's useful when you need to subtract a vector (in this case u) from all the elements along a particular dimension in a matrix (in this case data). The dimension along which the operation is being performed must match in both cases. For your example, you can incorporate the code without bsxfun as

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

    and so on for the other operations.

提交回复
热议问题