使用Julia使用in()和元组数组时出错 [英] Error using in() with an array of tuples using Julia
问题描述
我有2个元组数组,并且有一个循环询问一个元素是否在另一个元素中. 在每一步中,我都会询问coord数组中包含的元组是否在Y数组中.除了一个我无法解释原因的元素之外,该循环工作正常.这就是我所拥有的:
I have 2 arrays of tuples and I have a loop asking if one element is in the other. At each step I ask if the tuple contained in the coord array is in the Y array. The loop works fine except for one element which I cant explain why. Here is what I have :
Y[55:65] # This is the array I want to check at each step if my state is in or not.
11个元素的数组{Any,1}:(2.0,1.0)(3.0,1.0)(4.0,1.0)(5.0, 1.0)(6.0,1.0)(7.0,1.0)(8.0,1.0)(9.0,1.0)(10.0,1.0)(11.0,1.0)(12.0,1.0)
11-element Array{Any,1}: (2.0, 1.0) (3.0, 1.0) (4.0, 1.0) (5.0, 1.0) (6.0, 1.0) (7.0, 1.0) (8.0, 1.0) (9.0, 1.0) (10.0, 1.0) (11.0, 1.0) (12.0, 1.0)
coord[i-1] # this is one element of coord that is in Y
0维数组{元组{Float64,Float64},0} :( 6.0,1.0)
0-dimensional Array{Tuple{Float64,Float64},0}: (6.0, 1.0)
coord[i] # this is an other element of coord that is in Y
0维数组{元组{Float64,Float64},0} :( 7.0,1.0)
0-dimensional Array{Tuple{Float64,Float64},0}: (7.0, 1.0)
但是当我测试它们在Y位置时:
But then when I test when they are in Y:
in(coord[i],Y[55:65]) # testing if coord[i] is in Y
false
false
in(coord[i-1],Y[55:65]) # testing if coord[i-1] is in Y
true
true
我不明白:它们在Y中以相同的方式表示,它们具有相同的类型,为什么我要使用in()来获得一个在其中而不是另一个?
I dont understand: they are both represented in the same way in Y, they have the same type, why do I get from using in() that one is in and not the other?
我使用的是Julia版本0.6.3.
I use Julia version 0.6.3.
提前感谢您的帮助!
推荐答案
您是怎么得到coord
和Y
的?如果通过计算而不是直接分配获得它们,则即使它们显示正确,它们也可能不完全相同.例如:
How did you get coord
and Y
? If you get them by calculations rather than direct assignments, they may not be exactly equal even if they are displayed so. For example:
julia> p1 = fill((6.0, 1.0))
0-dimensional Array{Tuple{Float64,Float64},0}:
(6.0, 1.0)
julia> p2 = fill((7.0 + 3eps(), 1.0))
0-dimensional Array{Tuple{Float64,Float64},0}:
(7.000000000000001, 1.0)
julia> Y = [p1, p2]
2-element Array{Array{Tuple{Float64,Float64},0},1}:
(6.0, 1.0)
(7.0, 1.0) # NOTE that it get truncated in display but the content did not changed!
julia> x = fill((6.0, 1.0))
0-dimensional Array{Tuple{Float64,Float64},0}:
(6.0, 1.0)
julia> x in Y
true
julia> x = fill((7.0, 1.0))
0-dimensional Array{Tuple{Float64,Float64},0}:
(7.0, 1.0)
julia> x in Y
false
在这种情况下,您可以在比较之前对它们进行四舍五入,或者使用isapprox
(或≈
运算符,在\approx
+ Tab中在Julia中键入)手动编写in
函数
If this is the case, you can either round them before comparing or write the in
function mannually using isapprox
(or the ≈
operator, typed in Julia by \approx
+ Tab)
这篇关于使用Julia使用in()和元组数组时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!