我在运行下面的Julia代码片段时遇到错误 using GR, Interact
t = 0:0.01:1
@manipulate for phi=0:0.1:6.28
plot(cos.(2π*t+phi))
end
LoadError: MethodError: no method matching +(::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}, ::Float64)
Closest candidates are:
+(::Any, ::Any, !Match
朱莉娅中是否有一个像R的paste()函数一样的函数?特别是,如果我们给函数两个向量,它将返回一个向量与两个输入向量的元素级级联。
我环顾四周,似乎无法在文档或其他文件中找到答案。约翰·迈尔斯·怀特( John )的认为,朱莉娅的join()函数是最接近的类似函数,但它似乎只适用于字符串对,而不适用于字符串向量上的元素。
目前,我只是在调用join()的元素上使用下面的循环函数,但我想知道是否有更好的方法。
x = ["aa", "bb", "cc"]
y = ["dd", "ee", "ff"]
具有一个简单的函数,例如
function fun(x::Real, y::Real)
x, y
end
使用pmap()调用
pmap(fun, [x for x=0:.1:2], [y for y=4:-.1:2])
朱莉娅给出了这个错误
ERROR: LoadError: MethodError: Cannot `convert` an object of type Tuple{Float64,Float64} to an object of type AbstractFloat
This may have arisen from a call to the constructo
我尝试将一个字符串添加到数组中,如下所示: arry = String[]
append!(arry, "test") 但是我得到了以下错误: Cannot `convert` an object of type Char to an object of type String 为什么我在添加字符串而不是字符时会出现这个错误? (为了其他Julia新手的利益,在下面发布我自己的答案-任何有更多有用提示的人请加入!)
例如,我可以创建一个包含函数的数组。
julia> a(x) = x + 1
>> a (generic function with 1 method)
julia> [a]
>> 1-element Array{#a,1}:
a
但我似乎无法将该函数添加到空数组中:
julia> append!([],a)
>> ERROR: MethodError: no method matching length(::#a)
Closest candidates are:
length(::SimpleVector) at
有等同于Python的pop吗?我有一个数组x和一个长度相同的布尔数组flag。我希望提取x[flag]并能够将其存储在变量x_flagged中,同时将它们从x中删除。
x = rand(1:5, 100)
flag = x .> 2
x_flagged = some_function!(x, flag) # Now x would be equal to x[x .<= 2]
我试图用以下代码段在julia中生成12个字符的字母数字字符串:
a)
an = randstring(rand(Bool) ? ('A':'Z') : ('0':'9'), 12)
b)
an = ""
for i in [1:12]
an *= randstring(rand(Bool) ? ('A':'Z') : ('0':'9'))
end
但两者要么给出完整的12位数字,要么给出12个字母,但不给出它们的组合。
请指导我生成12个字
在Julia中,将变量定义为const与将变量定义为const global有什么区别?考虑到下面的示例,如果我将const global更改为const,有什么区别?
#set number of cores
number_cores=7;
addprocs(number_cores)
#include necessary functions
@everywhere include("$(pwd())\\lib.jl");
const global n = 2; # number of observables
const global k = nVAR + 2*n-1;
在C++中,我可以执行以下操作:
class foo {
private:
int N;
public:
foo(const int pN) {
N = pN;
std::cout << N << std::endl;
}
};
或者,考虑到朱莉娅中的外部构造者的概念,
class foo {
private:
int N;
};
foo::foo(const int pN) {
N = pN;
std::cout << N << std::endl;
}
你能在Julia中做同样的事情吗
我想键入一个字典,但是如果键不存在,Julia会抛出一个异常。为了避免异常,我必须首先检查它们的key是否存在于字典中。 我现在使用这个自定义函数: function has_some_key(dict, key)
for (k, v) in dict
if k == key
return true
end
end
return false
end 有没有更好的方法来确定字典是否有给定键的映射?
我在Python和Julia中实现了Mandelbrot函数;但是,在第6次迭代之后,代码产生了不同的结果。主要原因是什么?
以下是Python的代码:
def mandelbrot(a):
z = 0
for i in range(50):
z = z**2 + a
return z
下面是Julia中的相同代码:
function mandelbrot(a)
z = 0
for i=1:50
z = z^2 + a
end
return z
end
我正在尝试安装和使用DataStructures包,但它似乎不起作用;或者我遗漏了一些东西。
Pkg.init()
Pkg.status()
Pkg.add("DataStructures")
Pkg.status()
Pkg.update()
d = OrderedDict(Char,Int)
ERROR: OrderedDict not defined
有什么问题吗?
我有以下代码,给定i,我希望找到矩阵的i第四行。我的代码如下:
function f(mat,i)
println(mat[:i,:])
end
但是,我得到以下错误:
ArgumentError: invalid index: :i of type Symbol
我试过用i打印typeof的类型,它说是Int64。此外,如果我只想找到第一行,那么mat[:1,:]就会做这个工作,所以我认为问题不在于切片语法。
我很难让朱莉娅通过矩阵中的所有数字:
A = [1 -2 3; -4 -5 -6; 7 -8 9]
我想把所有的负数都变成正数。
我试过:
for i=A[1:end]
if i<0
A[i] = 3
i += 1
end
return (A)
end
我尝试过将i+=1移动到不同的位置。但还是没有改变什么。
我在Python木星中有以下代码:
n = 10**7
d = {}
%timeit for i in range(n): d[i] = i
%timeit for i in range(n): _ = d[i]
%timeit d[10]
有以下几次:
763 ms ± 19.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
692 ms ± 3.74 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
39.5 ns ± 0.186 ns per loop (mean
为什么这会引起LoadError: UndefVarError: T not defined
struct Point{T}
x::T
y::T
Point{T}(x,y) = new(x,y)
end
虽然这样做很好:
struct Point{T}
x::T
y::T
Point{T}(x,y) where {T} = new(x,y)
end
很久以前他们两人都很好:
编辑:为了澄清,我希望我们在struct Point{T}块中已经明确了T指的是什么,即使在第一种情况下也是如此。
我要从朱莉娅0.7调到1.0。茱莉亚关于变量范围的规则似乎从0.7变到1.0。例如,我想运行这样一个简单的循环:
num = 0
for i = 1:5
if i == 3
num = num + 1
end
end
print(num)
在Julia0.7(和大多数其他语言中)中,我们可以期待循环之后的num = 1。然而,它将招致朱莉娅1.0中的UndefVarError: num not defined。我知道通过使用let我可以做到这一点
let
num = 0
for i = 1:5
if i == 3
num = num +
以下代码失败。
global Θ=1.0
function f(a)
c=sin(a+θ)
return c
end
f(1)
UndefVarError: θ not defined
Stacktrace:
[1] f(a::Int64)
@ Main ./In[1]:3
[2] top-level scope
@ In[1]:6
[3] eval
@ ./boot.jl:373 [inlined]
[4] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::Stri
我想要形成这样的表达式
julia> ex = :(a[$i,$j] + b[$i,$j])
因为我对写一个程序感兴趣,它能够写出一个方程系统,在这个系统中,用户只给出它们的结构,然后julia显式地写出相关的代码块。更广泛地说,我希望在表达式中嵌入函数,例如
julia> ex = :(a[$myfunc(i,j),$j] + b[$i,$j])
在评估第一行代码时,我得到
ERROR: UndefVarError: i not defined
in eval(::Module, ::Any) at ./boot.jl:226
另一方面,如果我写下
julia> ex =