在Matlab中,当是最佳的使用bsxfun? [英] In Matlab, when is it optimal to use bsxfun?

查看:313
本文介绍了在Matlab中,当是最佳的使用bsxfun?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题:我已经注意到,在如此频繁使用的功能很多好的答案Matlab的问题的 bsxfun 。为什么呢?

My Question: I've noticed that a lot of good answers to Matlab questions on SO frequently use the function bsxfun. Why?

动机:在Matlab的文档 bsxfun ,下面的例子提供:

Motivation: In the Matlab documentation for bsxfun, the following example is provided:

A = magic(5);
A = bsxfun(@minus, A, mean(A))

当然,我们可以用做同样的操作:

Of course we could do the same operation using:

A = A - (ones(size(A, 1), 1) * mean(A));

而事实上,一个简单的速度测试演示了第二种方法是快20%左右。那么为什么要使用第一种方法?我猜有一些情况,其中使用 bsxfun 会比手动的方式快得多。我会在看到这种情况的一个例子,一个解释,为什么它是更快真正的兴趣。

And in fact a simple speed test demonstrates the second method is about 20% faster. So why use the first method? I'm guessing there are some circumstances where using bsxfun will be much faster than the "manual" approach. I'd be really interested in seeing an example of such a situation and an explanation as to why it is faster.

另外,从MATLAB文档中的最后一个元素这个问题,再次 bsxfun :C = bsxfun(好玩的,A,B)所适用的元素逐由函数手柄乐趣指定元素的二进制操作数组a和b,启用单扩张。什么是启用了单扩张这句话是什么意思?

Also, one final element to this question, again from the Matlab documentation for bsxfun: "C = bsxfun(fun,A,B) applies the element-by-element binary operation specified by the function handle fun to arrays A and B, with singleton expansion enabled.". What does the phrase "with singleton expansion enabled" mean?

推荐答案

有三方面的原因我使用 bsxfun 文档,的博客链接

There are three reasons I use bsxfun (documentation, blog link)


  1. bsxfun repmat 快(见下文)

  2. bsxfun 减少输入内容

  3. 使用 bsxfun ,就像使用 accumarray ,让我感觉良好,我的Matlab的理解。

  1. bsxfun is faster than repmat (see below)
  2. bsxfun requires less typing
  3. Using bsxfun, like using accumarray, makes me feel good about my understanding of Matlab.

bsxfun 将复制沿其单维度的输入数组,即尺寸沿该数组的大小为1,因此它们匹配的大小其他阵列的相应尺寸。这就是所谓的单expasion。顺便说一句,单件尺寸如果你调用紧缩

bsxfun will replicate the input arrays along their "singleton dimensions", i.e. the dimensions along which the size of the array is 1, so that they match the size of the corresponding dimension of the other array. This is what is called "singleton expasion". As an aside, the singleton dimensions are the ones that will be dropped if you call squeeze.

这可能是非常小的问题, repmat 办法是更快 - 但在那个数组的大小,无论操作是如此之快,可能不会做出任何在整体性能方面的差异。有两个重要的原因 bsxfun 速度更快:(1)计算发生在编译​​code,这意味着数组的实际复制不会发生,和(2 ) bsxfun 是多线程的Matlab的功能之一。

It is possible that for very small problems, the repmat approach is faster - but at that array size, both operations are so fast that it likely won't make any difference in terms of overall performance. There are two important reasons bsxfun is faster: (1) the calculation happens in compiled code, which means that the actual replication of the array never happens, and (2) bsxfun is one of the multithreaded Matlab functions.

我已经运行之间的速度对比 repmat bsxfun 与R2012b在我的体面快速的笔记本电脑。

I have run a speed comparison between repmat and bsxfun with R2012b on my decently fast laptop.

对于我来说, bsxfun repmat 快3倍左右。如果阵列得到较大的差变得更加明显

For me, bsxfun is about 3 times faster than repmat. The difference becomes more pronounced if the arrays get larger

repmat 运行时跳转发生1Mb的数组的大小,这可能是与我的处理器高速缓存的大小左右 - bsxfun 没有得到作为一个坏的跳跃,因为它仅需要分配输出数组。

The jump in runtime of repmat happens around an array size of 1Mb, which could have something to do with the size of my processor cache - bsxfun doesn't get as bad of a jump, because it only needs to allocate the output array.

下面你找到code口用于定时:

Below you find the code I used for timing:

n = 300;
k=1; %# k=100 for the second graph
a = ones(10,1);
rr = zeros(n,1);
bb=zeros(n,1);
ntt=100;
tt=zeros(ntt,1);
for i=1:n;
   r = rand(1,i*k);
   for it=1:ntt;
      tic,
      x=bsxfun(@plus,a,r);
      tt(it)=toc;
   end;
   bb(i)=median(tt);
   for it=1:ntt;
      tic,
      y=repmat(a,1,i*k)+repmat(r,10,1);
      tt(it)=toc;
   end;
   rr(i)=median(tt);
end

这篇关于在Matlab中,当是最佳的使用bsxfun?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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