Julia:通过多个布尔比较获得子数组 [英] Julia: getting subarray with multiple Boolean comparisons

查看:88
本文介绍了Julia:通过多个布尔比较获得子数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can we do the following Matlab code in Julia?

A=rand(4); 
A(:,A(1,:)>0.7)

我尝试过:

A=rand(4,4);
A[:,A[1,:].>0.7]

导致以下结果的错误:错误: getindex 没有与getindex(:: Array {Float64,2},:: UnitRange {Int64},:: BitArray {2})匹配的方法

Which results in: ERROR: getindex has no method matching getindex(::Array{Float64,2}, ::UnitRange{Int64}, ::BitArray{2})

推荐答案

** 编辑 **

茱莉亚(Julia)在3.5年中经历了许多变化,现在,OP的问题尚无定论.这种样式的数组索引现在可以正常工作.

Julia has gone through many changes in 3.5 years and now the OP's question is moot. That style of array indexing now works.

请参阅最新的 Julia Docs on数组索引以获取完整的方法列表.

See the current Julia Docs on Array Indexing for the full list of methods.

** 原始记录,用于历史记录 **

在Julia中, Array 索引必须使用 Vector (此处为布尔矢量)来完成.

In Julia, Arrayindexing needs to be done with a Vector (here with a boolean vector).

一种实现所需功能的方法是使用 vec 函数:

One way to accomplish what you want is to use the vec function:

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

另一种方法是:

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

这篇关于Julia:通过多个布尔比较获得子数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆