Julia : 数组(矩阵)的条件过滤

对于一个Julia数组而言,如果需要进行条件过滤,是否可以和Matlab一样方便?答案是肯定的。

一、用法

1、数值比较

julia> a =[1 2 3; 4 5 6; 7 8 9]
3x3 Array{Int64,2}:
 1  2  3
 4  5  6
 7  8  9

julia> a[a[:,1].>5,:]
1x3 Array{Int64,2}:
 7  8  9

julia> a[a[:,1].==4,:]
1x3 Array{Int64,2}:
 4  5  6

julia> b=a[a[:,1].>10,:]
0x3 Array{Int64,2}

julia> isempty(b)
true

2、字符串

julia> b =[1 "a" "c";2 "d" "e"]
2x3 Array{Any,2}:
 1  "a"  "c"
 2  "d"  "e"

julia> b[in("a",b[:,2]),:]
3-element Array{Any,1}:
 1
  "a"
  "c"

二、效率情况

julia> data =rand(800000,3);

julia> @time newone=data[data[:,1].>0.5,:];
  0.036659 seconds (568 allocations: 18.461 MB, 21.68% gc time)

julia> @time newone=data[data[:,1].>0.5,:];
  0.027426 seconds (414 allocations: 18.422 MB)

julia> @time newone=data[data[:,1].>0.5,:];
  0.031349 seconds (414 allocations: 18.422 MB, 15.93% gc time)

julia> size(newone)
(399871,3)

版权声明:本文为wowotuo原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。