删除Julia数组上的空列 [英] Remove null column on julia array
本文介绍了删除Julia数组上的空列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
与Julia一起,我希望删除具有0个值的列.我有一个像下面这样的数组,其中有很多我想删除的空列.
115×40 Array{Float64,2}:
-0.0 -0.0 -0.0 -0.0 … -0.0 0.0 -0.0
0.0 -0.0 -0.0 -0.0 0.0 0.0 0.0
-0.0 -0.0 -0.0 -0.0 -0.0 0.0 -0.0
0.0 0.0 -0.0 -0.0 -0.0 0.0 0.0
0.0 0.0 0.0 -0.0 -0.0 0.0 0.0
-0.0 1.0 -0.0 0.0 … -0.0 0.0 0.0
-0.0 -0.0 0.0 -0.0 -0.0 0.0 0.0
0.0 -0.0 -0.0 -0.0 0.0 0.0 0.0
0.0 -0.0 0.0 -0.0 -0.0 0.0 0.0
⋮ ⋱
0.0 1.0 -0.0 -0.0 0.0 -0.0 -0.0
-0.0 -0.0 0.0 -0.0 0.0 -0.0 -0.0
1.0 0.0 -0.0 -0.0 0.0 -0.0 0.0
-0.0 0.0 -0.0 -0.0 … 0.0 -0.0 -0.0
0.0 0.0 -0.0 0.0 -0.0 -0.0 -0.0
-0.0 -0.0 -0.0 0.0 -0.0 -0.0 -0.0
0.0 -0.0 -0.0 0.0 -0.0 0.0 0.0
-0.0 -0.0 -0.0 -0.0 -0.0 1.0 0.0
有人知道怎么做吗?
此致
解决方案
让a
为数组,然后
a[:, vec(mapslices(col -> any(col .!= 0), a, dims = 1))]
有效. mapslices
将a
简化为1x40的布尔矩阵,表示非零列,我们需要将其转换为Vector
进行索引,因此将vec
(或者,一个可能为dropdims
). /p>
取决于您的应用程序,视图而不是副本就足够了.
Beggining with Julia, I'm looking to remove the columns with 0 values. I have an array as bellow with a lot of null columns which I would like to remove.
115×40 Array{Float64,2}:
-0.0 -0.0 -0.0 -0.0 … -0.0 0.0 -0.0
0.0 -0.0 -0.0 -0.0 0.0 0.0 0.0
-0.0 -0.0 -0.0 -0.0 -0.0 0.0 -0.0
0.0 0.0 -0.0 -0.0 -0.0 0.0 0.0
0.0 0.0 0.0 -0.0 -0.0 0.0 0.0
-0.0 1.0 -0.0 0.0 … -0.0 0.0 0.0
-0.0 -0.0 0.0 -0.0 -0.0 0.0 0.0
0.0 -0.0 -0.0 -0.0 0.0 0.0 0.0
0.0 -0.0 0.0 -0.0 -0.0 0.0 0.0
⋮ ⋱
0.0 1.0 -0.0 -0.0 0.0 -0.0 -0.0
-0.0 -0.0 0.0 -0.0 0.0 -0.0 -0.0
1.0 0.0 -0.0 -0.0 0.0 -0.0 0.0
-0.0 0.0 -0.0 -0.0 … 0.0 -0.0 -0.0
0.0 0.0 -0.0 0.0 -0.0 -0.0 -0.0
-0.0 -0.0 -0.0 0.0 -0.0 -0.0 -0.0
0.0 -0.0 -0.0 0.0 -0.0 0.0 0.0
-0.0 -0.0 -0.0 -0.0 -0.0 1.0 0.0
Anyone knows how to do ?
Regards,
解决方案
Let a
be the array, then
a[:, vec(mapslices(col -> any(col .!= 0), a, dims = 1))]
works. mapslices
reduces a
to a 1x40 matrix of booleans, indicating the non-zero columns, and we need to convert that to a Vector
for indexing, hence vec
(alternatively, one could dropdims
).
Depending on you application, a view instead of a copy might be enough.
这篇关于删除Julia数组上的空列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文