# Eliminate types of elements from the list

I have list where only zeroes and other list elements are present. How can I eliminate only zeroes?

List is generated by:

xvar = Array[x, {n + 3, m + 3}, {{-2, n}, {-2, m}}] /.
x_[i_, j_] /; j > 0 -> 0
yvar = Array[y, {n + 3, m + 3}, {{-2, n}, {-2, m}}] /.
x_[i_, j_] /; j > 0 -> 0
vars = Join[Flatten[xvar], Flatten[yvar]]


I tried:

DeleteCases[vars, 0, Infinity]


but it deleted also indices of the x and y arrays.

Then I tried:

Assuming[{x[i_, j_] != 0 /; i > 0, x[i_, j_] != 0 /; j > 0, y[i_, j_] != 0 /; i > 0, y[i_, j_] != 0 /; j > 0}, vars2 = Table[If[vars[[i]] == 0, Nothing, vars[[i]]], {i, Length[vars]}]]


But it does not work - behaves as if the assumption is not there and leaves items like ''If[x[0, -2] == 0, Nothing, vars[[i]]]'' and is also cumberstone, I need some more filtering for xvar and yvar to apply.

• Maybe Pick[vars, NumericQ /@ vars, False] – Coolwater Oct 26 '17 at 17:31

The following works:

Select[vars, # =!= 0 &]


It selects elements in vars that are not 0. Your attempt with DeleteCases failed because of Infinity. Try DeleteCases[vars, 0, 1] instead, or equivalently

DeleteCases[vars, 0]


But this approach is apparently more computationally efficient:

Cases[vars, Except]


For $m=k=1000$, the first command runs in 0.75 seconds, the second in 0.26 and the third one in 0.14.

Edit For completeness, coolwater's suggestion Pick[vars, NumericQ /@ vars, False] returns the same result after 0.36 seconds.