小编Jon*_*nas的帖子

让索引返回一个矩阵而不是 julia 中的向量

当您在 Julia 中为矩阵建立索引,并且您的选择是单列或单行时,结果将表示为向量。类似地,当您索引单个点时,您将获得该点的值,而不是 1x1 矩阵。

但是,对于我的用例,我希望我的答案也是一个矩阵,因为向量的方向具有意义,我不想丢失。

因此,给出以下示例矩阵:

julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
 1  2
 3  4
Run Code Online (Sandbox Code Playgroud)

我得到:

julia> A[:, 1]
2-element Vector{Int64}:
 1
 3

julia> A[1, :]
2-element Vector{Int64}:
 1
 2

julia> A[1, 1]
 1
Run Code Online (Sandbox Code Playgroud)

但我想要:

julia> A[:, 1]
2×1 Matrix{Int64}:
 1
 3

julia> A[1, :]
1×2 Matrix{Int64}:
 1 2

julia> A[1, 1]
1×1 Matrix{Int64}:
 1
Run Code Online (Sandbox Code Playgroud)

有没有简单的方法来实现这一目标?

现在我做这样的事情:

function getindex_asmatrix(A::Matrix, i, j)
    # If i & j are Integers convert point to 1x1 Matrix
    # If …
Run Code Online (Sandbox Code Playgroud)

matrix julia

8
推荐指数
1
解决办法
65
查看次数

为什么除了我想抛出的错误之外,Julia 还抛出 LoadError

如果我编写一个除了抛出如下错误之外什么也不做的脚本:

throw(ErrorException("a useful message"))
Run Code Online (Sandbox Code Playgroud)

当我执行脚本时,出现以下错误。

throw(ErrorException("a useful message"))
Run Code Online (Sandbox Code Playgroud)

我想知道为什么我的错误消息中有一个LoadError:

我对其他错误类型也有同样的看法。例如:

throw(DivideError())
Run Code Online (Sandbox Code Playgroud)
$ julia throwError.jl
ERROR: LoadError: a useful message
Stacktrace:
 [1] top-level scope at /path/throwError.jl:1
 [2] include(::Function, ::Module, ::String) at ./Base.jl:380
 [3] include(::Module, ::String) at ./Base.jl:368
 [4] exec_options(::Base.JLOptions) at ./client.jl:296
 [5] _start() at ./client.jl:506
in expression starting at /path/throwError.jl:1
Run Code Online (Sandbox Code Playgroud)

error-handling julia

5
推荐指数
1
解决办法
1202
查看次数

如何在 Julia 中删除字符串

strip当我尝试在 Julia 中输入字符串时,为什么会出现错误?

strip("Clean Monkeys", "s")
# expected
> "Clean Monkey"
# but got 
> MethodError: objects of type String are not callable
Run Code Online (Sandbox Code Playgroud)

string strip julia

4
推荐指数
1
解决办法
3799
查看次数

我如何获得朱莉娅字典的大小

如何获得 Julia 字典的大小?size()抛出错误。

julia> d = Dict(:x => 1, :y => 2)
julia> size(d)
  MethodError: no method matching size(::Dict{Symbol,Int64})
Run Code Online (Sandbox Code Playgroud)

dictionary julia

3
推荐指数
1
解决办法
2884
查看次数

检查 DataFrame 名称是否包含另一个数组中的名称

我想检查我的 DataFrame 是否包含我指定的所有列。当然,我可以使用下面的代码来完成,但我觉得应该可以在一行中完成。

using DataFrames
bools = Array{Bool}([])
df = DataFrame(A=[1,2], B=[3,4], C=[5,6])
for name in ["A", "B"]
    push!(bools, name ? names(df))
end
false ? bools
Run Code Online (Sandbox Code Playgroud)

operators subset dataframe julia

3
推荐指数
1
解决办法
35
查看次数

如何在 julia 类型声明中定义类型元组

如何为 julia 函数声明特定类型的元组?

这有效:

function f(x, y)::Int8
    x+y
end

julia> f(2, 3)
  5
Run Code Online (Sandbox Code Playgroud)

这也有效:

function g(x, y)::Tuple
    x+y, x*y
end

julia> g(2, 3)
  (5, 6)
Run Code Online (Sandbox Code Playgroud)

但我不知道如何定义元组中的类型。

例如,这会引发错误:

function h(x, y)::Tuple(::Int8, ::Int8)
    x+y, x*y
end
  ERROR: syntax: invalid "::" syntax around REPL[48]:2
Run Code Online (Sandbox Code Playgroud)

这也是:

function k(x, y)::Tuple(Int8, Int8)
    x+y, x*y
end

julia> k(2, 3)
  ERROR: MethodError: no method matching Tuple(::Type{Int8}, ::Type{Int8})
Run Code Online (Sandbox Code Playgroud)

type-declaration julia

2
推荐指数
1
解决办法
76
查看次数

为什么 Julia 的 Dict{String, Any} 不接受“任何”值

我写了一个函数,它应该带一个Dict{String, <anything>}(所以有任何值)。但是,当我指定所需的参数时,Dict{String, Any}该函数仅采用显式的Dict{String, Any}. 有没有办法让 aDict取任何值?

function funky(arg::Dict{String, Any})
    return "Oh, hi mark."
end
Run Code Online (Sandbox Code Playgroud)
julia> funky(Dict("Oh" => [0 1; 2 3]))
ERROR: MethodError: no method matching funky(::Dict{String,Array{Int64,2}})
Closest candidates are:
  funky(::Dict{String,Any})

julia> funky(Dict{String, Any}("Oh" => [0 1; 2 3]))
"Oh, hi mark."
Run Code Online (Sandbox Code Playgroud)

parameters dictionary julia

2
推荐指数
1
解决办法
54
查看次数