Julia: Array元素过滤、元素替代、元素删除等酸爽操作

一、过滤:一个约束条件(略复杂的情况)
找出data中,满足第三列的元素在vet中的元素。

julia>data =[1 2 3; 
             4 5 6 ;
             7 8 9 ;
             10 11 12];

julia>vet =[6 ,9];

julia>condition =map(x->in(x,vet),data[:,3]);

julia>data[condition,:]
2×3 Array{Int64,2}:
 4  5  6
 7  8  9

还有其它方法么?

二、过滤:两个约束条件的情况

第一列中值为4,第二列中值>=5的

julia>data[(data[:,2].>=5 & data[:,1].==4),:]
1×3 Array{Int64,2}:
 4  5  6
 ```
 几种error的情况:

 ```
julia> data[(data[:,2].>=5 & data[:,1]==4),:] # error! =>.

julia> data[data[:,2].>=5 & data[:,1].==4,:] # error!=>(),用括号理好&的优先次序

julia> data[(data[:,2].>=5 && data[:,1]==4),:] # error! =>&&

三、元素替代

1、把data中的元素为2的,全部改为0

julia> data =[1,2,3,4,5,2,3,1]
8-element Array{Int64,1}:
 1
 2
 3
 4
 5
 2
 3
 1

julia> data[data.==2] .=0
2-element view(::Array{Int64,1}, [2, 6]) with eltype Int64:
 0
 0

julia> data
8-element Array{Int64,1}:
 1
 0
 3
 4
 5
 0
 3
 1

2、如果要把以下data中“”,用0代替,有什么操作方式?

julia> data
9×12 Array{Any,2}:
 8     ""   ""   ""   ""   ""   ""   ""   ""  ""  ""  ""
  ""   ""  3    6     ""   ""   ""   ""   ""  ""  ""  ""
  ""  7     ""   ""  9     ""  2     ""   ""  ""  ""  ""
  ""  5     ""   ""   ""  7     ""   ""   ""  ""  ""  ""
  ""   ""   ""   ""  4    5    7     ""   ""  ""  ""  ""
  ""   ""   ""  1     ""   ""   ""  3     ""  ""  ""  ""
  ""   ""  1     ""   ""   ""   ""  6    8    ""  ""  ""
  ""   ""  8    5     ""   ""   ""  1     ""  ""  ""  ""
  ""  9     ""   ""   ""   ""  4     ""   ""  ""  ""  ""

只需要:

julia> data[data .== ""] .= 0  # 其中两个点,均不能少!
julia> data
9×12 Array{Any,2}:
 8  0  0  0  0  0  0  0  0  0  0  0
 0  0  3  6  0  0  0  0  0  0  0  0
 0  7  0  0  9  0  2  0  0  0  0  0
 0  5  0  0  0  7  0  0  0  0  0  0
 0  0  0  0  4  5  7  0  0  0  0  0
 0  0  0  1  0  0  0  3  0  0  0  0
 0  0  1  0  0  0  0  6  8  0  0  0
 0  0  8  5  0  0  0  1  0  0  0  0
 0  9  0  0  0  0  4  0  0  0  0  0

猜一猜如果:

julia> data[data .= ""] .= 0 # bug

四、把Array 拆分成几个小Array

方法一: map

julia> data
9×9 Array{Any,2}:
 8  0  0  0  0  0  0  0  0
 0  0  3  6  0  0  0  0  0
 0  7  0  0  9  0  2  0  0
 0  5  0  0  0  7  0  0  0
 0  0  0  0  4  5  7  0  0
 0  0  0  1  0  0  0  3  0
 0  0  1  0  0  0  0  6  8
 0  0  8  5  0  0  0  1  0
 0  9  0  0  0  0  4  0  0
尝试一:失败
A =map((p,q)->data[1:3,p:q], 1:3:7, 3:3:9) # 
3-element Array{Array{Any,2},1}:
 [8 0 0; 0 0 3; 0 7 0]
 [0 0 0; 6 0 0; 0 9 0]
 [0 0 0; 0 0 0; 2 0 0]

julia> A[1]
3×3 Array{Any,2}:
 8  0  0
 0  0  3
 0  7  0
julia> A[2]
3×3 Array{Any,2}:
 0  0  0
 6  0  0
 0  9  0

julia> A[3]
3×3 Array{Any,2}:
 0  0  0
 0  0  0
 2  0  0

有没有办法,一口气拆成N个 3x3的矩阵?

为什么才3个元素? 因为是一一对应的关系,[1,4,7]<=>[1,4,7],不是循还。

尝试2:失败
julia> A =map((p,q)->data[p:p+2,q:q+2], [1,4,7],[1,4,7])
3-element Array{Array{Any,2},1}:
 [8 0 0; 0 0 3; 0 7 0]
 [0 0 7; 0 4 5; 1 0 0]
 [0 6 8; 0 1 0; 4 0 0]

julia> A[1]
3×3 Array{Any,2}:
 8  0  0
 0  0  3
 0  7  0

尝试3: 成功!双重map + flatten

 julia> t =map(p->map(q->data[p:p+2,q:q+2],[1,4,7]),[1,4,7]) 
3-element Array{Array{Array{Any,2},1},1}:
 [[8 0 0; 0 0 3; 0 7 0], [0 0 0; 6 0 0; 0 9 0], [0 0 0; 0 0 0; 2 0 0]]
 [[0 5 0; 0 0 0; 0 0 0], [0 0 7; 0 4 5; 1 0 0], [0 0 0; 7 0 0; 0 3 0]]
 [[0 0 1; 0 0 8; 0 9 0], [0 0 0; 5 0 0; 0 0 0], [0 6 8; 0 1 0; 4 0 0]]

julia> collect(Iterators.flatten(t))
9-element Array{Array{Any,2},1}:
 [8 0 0; 0 0 3; 0 7 0]
 [0 0 0; 6 0 0; 0 9 0]
 [0 0 0; 0 0 0; 2 0 0]
 [0 5 0; 0 0 0; 0 0 0]
 [0 0 7; 0 4 5; 1 0 0]
 [0 0 0; 7 0 0; 0 3 0]
 [0 0 1; 0 0 8; 0 9 0]
 [0 0 0; 5 0 0; 0 0 0]
 [0 6 8; 0 1 0; 4 0 0]

尝试4:成功!大杀器,把repeat也搬出来
julia>using  DelimitedFiles;
julia> map((i,j)->data[i:i+2,j:j+2],repeat([1,4,7],inner=3),repeat([1,4,7],outer=3))
9-element Array{Array{Any,2},1}:
 [8 0 0; 0 0 3; 0 7 0]
 [0 0 0; 6 0 0; 0 9 0]
 [0 0 0; 0 0 0; 2 0 0]
 [0 5 0; 0 0 0; 0 0 0]
 [0 0 7; 0 4 5; 1 0 0]
 [0 0 0; 7 0 0; 0 3 0]
 [0 0 1; 0 0 8; 0 9 0]
 [0 0 0; 5 0 0; 0 0 0]
 [0 6 8; 0 1 0; 4 0 0]

2、列表推导式

简单,快捷。
尝试5: 成功!

julia> B=[data[p:p+2,q:q+2] for p in [1,4,7] for q in [1,4,7]]
9-element Array{Array{Any,2},1}:
 [8 0 0; 0 0 3; 0 7 0]
 [0 0 0; 6 0 0; 0 9 0]
 [0 0 0; 0 0 0; 2 0 0]
 [0 5 0; 0 0 0; 0 0 0]
 [0 0 7; 0 4 5; 1 0 0]
 [0 0 0; 7 0 0; 0 3 0]
 [0 0 1; 0 0 8; 0 9 0]
 [0 0 0; 5 0 0; 0 0 0]
 [0 6 8; 0 1 0; 4 0 0]

五、Array中元素的删除

1、filter!

把b中的为3的元素删除

julia> b =[1,2,3,4,5,6]
6-element Array{Int64,1}:
 1
 2
 3
 4
 5
 6

julia> filter!(x->x!=3,b)
5-element Array{Int64,1}:
 1
 2
 4
 5
 6

2、deleteat!

也可以,大家可以试一下。删除特定位置的元素

julia> deleteat!([6, 5, 4, 3, 2, 1], 1:2:5)
3-element Array{Int64,1}:
 5
 3
 1

你可能感兴趣的:(Julia)