Remove null column on julia array

岁酱吖の 提交于 2021-01-28 14:36:23

问题


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,


回答1:


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.



来源:https://stackoverflow.com/questions/53683556/remove-null-column-on-julia-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!