我已经在文档和论坛中徘徊了一段时间,我还没有找到一个内置的方法/函数来完成删除数组中元素的简单任务.有这样的内置功能吗?
我要求相当于python的list.remove(x).
这是一个天真地从框中选择一个函数的例子:
julia> a=Any["D","A","s","t"]
julia> pop!(a, "s")
ERROR: MethodError: `pop!` has no method matching
pop!(::Array{Any,1}, ::ASCIIString)
Closest candidates are:
pop!(::Array{T,1})
pop!(::ObjectIdDict, ::ANY, ::ANY)
pop!(::ObjectIdDict, ::ANY)
...
Run Code Online (Sandbox Code Playgroud)
这里提到使用 deleteat!,但也不起作用:
julia> deleteat!(a, "s")
ERROR: MethodError: `-` has no method matching -(::Int64, ::Char)
Closest candidates are:
-(::Int64)
-(::Int64, ::Int64)
-(::Real, ::Complex{T<:Real})
...
in deleteat! at array.jl:621
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建10个独特随机整数的元素数组.但是我无法创建具有唯一值的数组.朱莉娅有像Pythons 样本函数吗?
numbers = zeros(Array(Int64, 10))
rand!(1:100, numbers)
Run Code Online (Sandbox Code Playgroud)
谢谢.
当试图编写类型稳定的代码,检查使用时@code_warntype,我应该只担心没有任何Any或Union在变量部分或我还应该检查正文部分?
有没有办法可以在函数内本地化记忆(通过 Memoize.jl)?或者至少删除通过记忆创建的词典?
澄清:假设我定义了一个定义函数 f(x, y)。我想为 y 的每个新值创建一个新表。也就是说,给定 y = y0,f( . , y0) 对 x、x-1 等进行自身迭代,但给定一个新的 y = y1,我不需要为 y0 存储旧表,这样内存就可以被释放。我怎样才能做到这一点?
解决方案:
cachedfib() = begin
global dict = Dict()
global dict2 = Dict()
function _fib(n::Int, a::Int)
if !haskey(dict2, a)
dict2[a] = true
dict = Dict()
end
if haskey(dict, (n, a))
return dict[(n, a)]
elseif n < 2
dict[(0, a)] = 0
dict[(1, a)] = 1
return dict[(n, a)]
else
dict[(n, a)] = a*(_fib(n - 1, a) + _fib(n - 2, …Run Code Online (Sandbox Code Playgroud) 我想创建一个关联矩阵.
我有一个包含3列的文件,例如:
id x y
A 22 2
B 4 21
C 21 360
D 26 2
E 22 58
F 2 347
Run Code Online (Sandbox Code Playgroud)
我想要一个矩阵(没有col和row名称):
2 4 21 22 26 58 347 360
A 1 0 0 1 0 0 0 0
B 0 1 1 0 0 0 0 0
C 0 0 1 0 0 0 0 1
D 1 0 0 0 1 0 0 0
E 0 0 0 1 0 1 0 0
F 1 0 …Run Code Online (Sandbox Code Playgroud) 假设我用123播种srand(123),并运行rand()X次.后来,我希望能够重新启动Julia并播种一个数字(或状态),这样当我rand()再次运行时,如果我有种子123并运行rand()X + 1次,我将获得生成的数字.有什么方法可以做到这一点,还是我真的必须运行rand()X次以获得我想要的状态?
访问(也许替换)大型多维数组中的条目的最有效方法是什么?我在循环中使用这样的东西:
tup = (16,45,6,40,3)
A[tup...] = 100
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有更有效的方法.特别是,有什么方法可以避免使用...?
有谁知道在Julia中计算超几何函数的包?
我一直在使用GSL.jl,它是GNU Scientific Library的包装器,但GSL只支持0F0,0F1,1F1,2F0和2F1.我需要计算3F2.
朱莉娅的新手,很困惑.
这是一个数组:
array=["a","b",1]
Run Code Online (Sandbox Code Playgroud)
我定义了一本字典
dict=Dict()
dict["a","b"]=1
Run Code Online (Sandbox Code Playgroud)
我想用'array'来定义dict
dict2 = Dict()
dict2[array[1:2]]=1
Run Code Online (Sandbox Code Playgroud)
但他们不一样,
julia> dict
Dict{Any,Any} with 1 entry:
("a","b") => 1
julia> dict2
Dict{Any,Any} with 1 entry:
Any["a","b"] => 1
Run Code Online (Sandbox Code Playgroud)
如何使用'array'生成'dict'而不是'dict2'?谢谢
用f(x::Real)和定义函数之间有什么区别f{T <: Real}(x::T)吗?
@code_warntype 给出相同的输出
function f(x::Real)
x^2
end
function g{T <: Real}(x::T)
x^2
end
Run Code Online (Sandbox Code Playgroud)