问题
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