linspace 相对于冒号的优势是什么“:"?操作员? [英] What is the advantage of linspace over the colon ":" operator?

查看:122
本文介绍了linspace 相对于冒号的优势是什么“:"?操作员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

写作有什么好处

t = linspace(0,20,21)

结束

t = 0:1:20

?

我知道前者会产生一个向量,就像第一个一样.
谁能告诉我 linspacet = 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.

想象一下,您需要为直方图定义 bin 边缘.尤其是你需要特定的 bin 边缘 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

不定义右 bin 边缘,但是:

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避免,因为一个整数的除法不是那么微妙,就像浮点数的累积和一样.但正如马克·迪金森在评论中指出的那样:您不应该依赖任何计算出的值正是您所期望的.这不是 linspace 的用途. 在我看来,这取决于您遇到浮点问题的可能性有多大,以及您可以将它们的概率降低多少,或者您可以将容差设置得有多小.使用 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.

这是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 尝试确保(顾名思义)线性间距,而 colon 尝试确保对称

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 没有 的优势(除了 可用性),但是当涉及到浮点精细任务时,可能会有.

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.

Sam Roberts 的回答 提供了一些额外的信息并进一步澄清了一些事情,包括 MathWorks 的一些声明 关于冒号运算符.

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

这篇关于linspace 相对于冒号的优势是什么“:"?操作员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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