问题描述
我正在尝试在Matlab中使用linspace来处理较小的数字,例如从0.00003到0.1.
I am trying to use linspace in Matlab for small numbers, e.g. from 0.00003 to 0.1.
但是,如果执行此操作,则第一个数字/bin不是0.00003,而是0,这不会给我带来均等的分布:
However, if I do this, the first number/bin is not 0.00003, but 0, which does not give me an equal distribution:
ans =
0.0000 0.0111 0.0222 0.0334 0.0445 0.0556 0.0667 0.0778 0.0889 0.1000
我意识到,如果我从0.0003或更大的数字开始就可以使用,但是如何使它适用于较小的数字呢?
I realized that if I start with 0.0003 or larger then it works, but how can I make it work for smaller numbers?
推荐答案
这完全是由于 MATLAB命令窗口正在显示您的数据.
显示数字的默认方式是 short
格式文档指出:
The default way that numbers are displayed is the short
format which the documentation states is:
您的第一个数据点直到小数点后第5位都没有非零数字,因此它简单地显示为0.0000
.
Your first data point doesn't have a non-zero digit until the 5th digit after the decimal point so it simply shows up as 0.0000
.
尝试将显示格式更改为将显示的内容更重要的数字.您可以使用format
进行此操作.
Try changing the display format to something that will show more significant digits. You can do this using format
.
format long g
此外,将来,如果您实际上要检查是否按预期运行,则可以通过值进行显式检查,而不仅仅是相信命令窗口中显示的内容.
Also, in the future, if you actually want to check that something is behaving as you expect, do an explicit check by value not just by trusting what shows up in the command window.
limit = 0.000003
data = linsapce(limit, 0.1, 10);
% Check that the first datapoint is "equal" to what you expect
assert(abs(data(1) - limit) < eps)
这篇关于如何使linspace适用于少量数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!