English 中文(简体)
这是否与次2ind/ind2sub有关?
原标题:Is this a bug relating to sub2ind / ind2sub?
  • 时间:2012-04-18 15:06:34
  •  标签:
  • matlab

我正试图根据我跨越以下奇怪行为而得出的单方变量进行分化。 例如,当我尝试:

[a b] = ind2sub([50000 50000], sub2ind([50000 50000], single(1000), single(1000)))

我收到了:

a = 1001
b = 1000

难道这只是一ug,还是我失踪了吗? 我知道,这可能是因为在肉体法典中有些地方出现过大量流,但是它是否应当发生?

我从64比特(glnxa64)R2012a、R2011a、R2010b、R2010a那里获得了同样的错误行为,但从32比特(glnx86)R2010b中获得了正确结果。

最佳回答

The reason this happens is the following.

On line 35 of ind2sub.m is

vi = rem(ndx-1, k(i)) + 1;   

如果是代数,则采用指数。 因此,从次2英德的电话来看,第二位数为49951000。 现在,当你以单一精确值计算时,它就迫使它只精确地评估所有数学。 因此,比较了13.35年发生的不同情况。

K>> 49951000-1
ans =
        49950999

K>> single(49951000)-1
ans =
        49951000

这个问题比很多人少一些。 因此,这只字塔是个灯塔,它限制了单层浮点的精确度。 这可能有助于某些方面。

edit:正如Rasman所指出的,显示这一点的一个绝佳途径是eps

eps(single(49951000))=4

因此,单项(49951000)的增值或减去值(4-4)后,由于单项折旧的准确性,将退回495100美元。

问题回答

Been thinking some more, especially looking a gnovice s comment which sparked everything.

这里就这样说:单一是32个经签字的浮动点。 因此,对你获得数据的确切程度有限制。

<代码>eps (single (49951000) = 4,而

Thus your range is off, and you will get quantization especially as you increase the values of the indices:

for i = 1000:1010
    sub2ind([50000 50000], single(i), single(1000))
end

如果你真的想要有32个比数,我建议你使用uint32,因为你能够代表所有数据点(50000号令的固定基数为2.5e+09数据点;2^32(=4.3e9))。

for i = 1000:1010
    sub2ind([50000 50000], uint32(i), uint32(1000))
end




相关问题
MATLAB Solving equations problem

I want to solve these equations using MATLAB and I am sure there is a non zero solution. The equations are: 0.7071*x + 0.7071*z = x -0.5*x + 0.7071*y + 0.5*z = y -0.5*x - 0.7071*y +...

Difference between MATLAB s matrix notations

How do you read the following MATLAB codes? #1 K>> [p,d]=eig(A) // Not sure about the syntax. p = 0.5257 -0.8507 -0.8507 -0.5257 d = ...

preventing data from being freed when vector goes out of scope

Is there a way to transfer ownership of the data contained in a std::vector (pointed to by, say T*data) into another construct, preventing having "data" become a dangling pointer after the vector goes ...

Divide two polynomials using MATLAB

I want to divide p(x) by q(x) given that: p(x)=-5x^4+3x^2-6x q(x)=x^2+1 I tried: p=inline( -5*(x^4)+3*(x^2) , x ) p = Inline function: p(x) = -5*(x^4)+3*(x^2) q=inline( x^2+1 , x ) q = ...

matlab deals with Fibbonacci

The Fibonacci series is given as follows: 1, 2, 3, 5, 8, 13, 21, ... How can I write a script which calculates and prints the n-th Fibonacci term (for n>2), where n is inputed by the user. This ...

How do I plot lines between all points in a vector?

I have a vector containing some points in 2-D space. I want MATLAB to plot these points with lines drawn from every point to every other point. Basically, I want a graph with all vertices connected. ...

How do I create a string using a loop variable in MATLAB?

I have a loop like this: for i=1:no %some calculations fid = fopen( c:\out.txt , wt ); %write something to the file fclose(fid); end I want data to be written to different files like ...

热门标签