我有一系列颜色,我想将其转换为数字矩阵:
using Colors
cols = [RGB{Float64}(rand(), rand(), rand()) for i in 1:6]
6-element Array{ColorTypes.RGB{Float64},1}:
RGB{Float64}(0.836012,0.505908,0.249548)
RGB{Float64}(0.383172,0.105153,0.361422)
RGB{Float64}(0.680616,0.974232,0.942787)
RGB{Float64}(0.804829,0.825503,0.990222)
RGB{Float64}(0.0404051,0.569093,0.772053)
RGB{Float64}(0.872298,0.704112,0.473588)
转换为:
6×3 Array{Float64,2}:
0.836012 0.505908 0.249548
0.383172 0.105153 0.361422
0.680616 0.974232 0.942787
0.804829 0.825503 0.990222
0.0404051 0.569093 0.772053
0.872298 0.704112 0.473588
我该怎么做?
答案 0 :(得分:2)
使用reinterpret
。它“使用与给定数组相同的二进制数据构造一个数组,但具有指定的元素类型。”这意味着它以相同的顺序读取数据 - 并记住Julia是专栏。它也不知道返回的数组应该是什么形状,因此默认情况下它只是一个向量:
julia> reinterpret(Float64, cols)
18-element Array{Float64,1}:
0.836012
0.505908
0.249548
0.383172
0.105153
⋮
您可以看到它已拉出浮点值并将它们全部放在平面向量中[c₁,c₂]
变为[r₁, g₁, b₁, r₂, g₂, b₂]
。所以你想首先得到一个尊重这种结构的3x6阵列:
julia> fs = reinterpret(Float64, cols, (3, length(cols)))
3x6 Array{Float64,2}:
0.836012 0.383172 0.680616 0.804829 0.0404051 0.872298
0.505908 0.105153 0.974232 0.825503 0.569093 0.704112
0.249548 0.361422 0.942787 0.990222 0.772053 0.473588
现在,如果需要,您可以通过转置来获得所需的形状:
julia> fs'
6x3 Array{Float64,2}:
0.836012 0.505908 0.249548
0.383172 0.105153 0.361422
0.680616 0.974232 0.942787
0.804829 0.825503 0.990222
0.0404051 0.569093 0.772053
0.872298 0.704112 0.473588
答案 1 :(得分:1)
一种方法是:
try{
//print something here
Thread.sleep(3000); //sleep for 3 seconds
//print something else here
}
catch(InterruptedException e){
System.out.println("got interrupted!");
}