什么是linspace在结肠&QUOT的优点;:"运营商? [英] What is the advantage of linspace over the colon ":" operator?

查看:188
本文介绍了什么是linspace在结肠&QUOT的优点;:"运营商?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有写一些优势

t = linspace(0,20,21)

t = 0:1:20

据我所知,前者产生的载体,作为第一个做。结果
任何人都可以说出我一些情况,其中 linspace 是非常有用的结束 T = 0:1:20

I understand the former produces a vector, as the first does.
Can anyone state me some situation where linspace is useful over t = 0:1:20?

推荐答案

这不仅仅是实用性。虽然文件说:

It's not just the usability. Though the documentation says:

linspace 函数生成线性间隔矢量。它是
  类似冒号运算符:,但给出了直接控制
  点的数量。

The linspace function generates linearly spaced vectors. It is similar to the colon operator :, but gives direct control over the number of points.

是一样的, linspace 的主要区别和优势是,它产生的的整数具有所需长度的矢量(或默认100)和扩展它的之后以所需的范围。在结肠直接增量创建矢量

it is the same, the main difference and advantage of linspace is that it generates a vector of integers with the desired length (or default 100) and scales it afterwards to the desired range. The : colon creates the vector directly by increments.

想象一下,你需要定义箱边的直方图。特别是你需要一定的仓边 0.35 来正好在它的正确的地方:

Imagine you need to define bin edges for a histogram. And especially you need the certain bin edge 0.35 to be exactly on it's right place:

edges = [0.05:0.10:.55];
X = edges == 0.35

edges =   0.0500    0.1500    0.2500    0.3500    0.4500    0.5500
X =  0     0     0     0     0     0

不定义权斌优势,但:

does not define the right bin edge, but:

edges = linspace(0.05,0.55,6);   %// 6 = (0.55-0.05)/0.1+1
X = edges == 0.35

edges =   0.0500    0.1500    0.2500    0.3500    0.4500    0.5500
X =  0     0     0     1     0     0

一样。

那么,它基本上是一个浮点问题。其中的可以 linspace 避免,因为一个整数的除法是不是细腻,像累积和的floting点数。但正如马克狄金森在评论中指出:
你不应该依赖任何关于计算的值是你期望什么的。这不是什么linspace是。的在我看来,这是你将如何有可能得到浮点问题的问题,多少可以减轻probabilty他们或你多么小可以设置的公差。使用linspace 可以减少这些问题的次数的概率,它不是一个安全性。

Well, it's basically a floating point issue. Which can be avoided by linspace, as a single division of an integer is not that delicate, like the cumulative sum of floting point numbers. But as Mark Dickinson pointed out in the comments: You shouldn't rely on any of the computed values being exactly what you expect. That is not what linspace is for. In my opinion it's a matter of how likely you will get floating point issues and how much you can reduce the probabilty for them or how small can you set the tolerances. Using linspace can reduce the probability of occurance of these issues, it's not a security.

这是的code linspace

n1 = n-1
c = (d2 - d1).*(n1-1) % opposite signs may cause overflow
if isinf(c)
    y = d1 + (d2/n1).*(0:n1) - (d1/n1).*(0:n1)
else
    y = d1 + (0:n1).*(d2 - d1)/n1
end

要总结: linspace 和结肠都在做不同的任务可靠。 linspace 将尽力确保(顾名思义)直线间距,而大肠努力确保对称

To sum up: linspace and colon are reliable at doing different tasks. linspace tries to ensure (as the name suggests) linear spacing, whereas colon tries to ensure symmetry

在你的特殊情况,为您创建一个整数向量,有 linspace 的优势(除了的usability ),但是当涉及到​​浮点微妙的任务,也有可能是。

In your special case, as you create a vector of integers, there is no advantage of linspace (apart from usability), but when it comes to floating point delicate tasks, there may is.

山姆·罗伯茨的答案提供了一些额外的信息,并进一步澄清事情,包括MathWorks公司<一的某些陈述href=\"http://www.mathworks.co.uk/matlabcentral/answers/143255-how-does-the-colon-operator-work\">regarding冒号运算符。

The answer of Sam Roberts provides some additional information and clarifies further things, including some statements of MathWorks regarding the colon operator.

这篇关于什么是linspace在结肠&QUOT的优点;:&QUOT;运营商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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