For anyone interested in doing this kind of thing, I would advise trying to circumvent it - both options dmolina and yha show do the job, but they use a very large amount of allocations around 250k to 500k respectively to achieve it. I instead wrote out a bunch of if statements and got a lot lower allocation count.
function filter_out_all_zero_columns!(X)
cnt = 1
for i in axes(X, 2)
zc = false
for j in axes(X, 1)
if X[j, i] == 0
zc = true
break
end
end
if !zc & (cnt != i)
for j in axes(X, 1)
X[j, cnt] = X[j, i]
end
cnt += 1
end
end
X[:, 1:cnt - 1]
end
Because what you are doing is manipulating columns as units of data, so it is better to store them that way. Using SVectors or tuples, the compiler will know the number of elements in each, and generate very efficient code.
As a bonus, you can filter it like any other vector.
My experience has always been that using tuple’s is slightly more efficient than SVectors, so will try to stick wih them in the future, whenever I can.
Sometimes I just forget to think about how I am storing data etc.
This is one of the things I like about Julia is that one can write code at very many different levels of complexity - I had never seen the axes function before now.