Julia:获取具有多个布尔比较的子数组 [英] Julia: getting subarray with multiple Boolean comparisons

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

问题描述

How can we do the following Matlab code in Julia?

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

I've tried:

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

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

解决方案

** EDIT **

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.

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

** Original, for the History Books **

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

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

Another way is:

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天全站免登陆