朱莉娅:通过多次布尔比较获得子阵列

Arm*_*irn 2 indexing boolean julia

我们如何在Julia中执行以下Matlab代码?

A=rand(4); 
A(:,A(1,:)>0.7)
Run Code Online (Sandbox Code Playgroud)

我试过了:

A=rand(4,4);
A[:,A[1,:].>0.7]
Run Code Online (Sandbox Code Playgroud)

导致:错误:getindex没有方法匹配getindex(:: Array {Float64,2},:: UnitRange {Int64},:: BitArray {2})

ric*_*2hs 5

**编辑**

朱莉娅在3.5年中经历了许多变化,现在OP的问题没有实际意义.这种样式的数组索引现在有效.

有关完整的方法列表,请参阅当前Julia Docs on Array Indexing.

**原创,历史书籍**

在Julia中,Array索引需要使用Vector(这里使用布尔向量)来完成.

实现您想要的一种方法是使用该vec功能:

julia> A = rand(4,4)
4x4 Array{Float64,2}:
 0.0253057  0.748903  0.633581  0.796249
 0.25894    0.330299  0.668624  0.723979
 0.884216   0.521359  0.957751  0.207386
 0.862909   0.286173  0.592699  0.965437

julia> A[:, vec(A[1,:] .> 0.7)]
4x2 Array{Float64,2}:
 0.748903  0.796249
 0.330299  0.723979
 0.521359  0.207386
 0.286173  0.965437
Run Code Online (Sandbox Code Playgroud)

另一种方式是:

julia> A[:, A[1,:][:] .> 0.7]
4x2 Array{Float64,2}:
 0.748903  0.796249
 0.330299  0.723979
 0.521359  0.207386
 0.286173  0.965437
Run Code Online (Sandbox Code Playgroud)