Matlab的bsxfun()代码 [英] Matlab's bsxfun() code

查看:89
本文介绍了Matlab的bsxfun()代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是做什么的?

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);

在没有bsxfun的情况下是否有等效的方法?

Is there an equivalent way of doing this without bsxfun?

推荐答案

函数 BSXFUN 将通过复制两个输入参数的尺寸,使其大小相互匹配,来执行请求的按元素操作(函数句柄参数).在这种情况下,您可以通过复制变量us自己使用函数 REPMAT 进行设置与data相同的大小.然后,您可以使用标准的逐元素算术运算符:

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

这篇关于Matlab的bsxfun()代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆