# How to delete lines containing specific values from a matrix? [closed]

I am doing some numerical computations and a few iterations return values that are obviously incorrect (some numerical integration problem, I suppose.). I have managed to set these values to zero but this does not goes well with the ListLinePlot I'm making. So, given this context, my problem is the following:

I have a nX2 matrix where n is a fairly large number. A few elements in column 2 are zero and I want to delete them. If there was only one zero element I would identify the position j of the zero and use Delete[matrix,j]. Since I have more than one of these I've been using

For[j = 1, j <= Length[matrix], j = j + 1,
If[matrix[[j, 2]] == 0, matrix = Delete[matrix, j]]
]


The problem is that this routine only deletes the few first lines with zeros and then stop doing that. I imagine it has something to do with Length[matrix] being changed, but I'm not sure. Could ou help me, please?

Thank you very much.

• Have you tried Select? Select[matrix, #[[2]] != 0 &] – BlacKow May 9 '16 at 16:53
• ...or Cases[]? – J. M.'s technical difficulties May 9 '16 at 16:54
• ... or DeleteCases[mat, {_, 0}] or Pick[mat, Unitize[mat[[;; , 2]]], 1]? – kglr May 9 '16 at 17:01
• Thank you so much. Select worked like a charm. Could you help me, though, to understand the syntax? What are the roles of # and &? Thank you very much. – Gabu May 9 '16 at 17:49
• @Gabu Take a look at this post. The #& is slot/pure function notation. A slot is unnamed argument. – BlacKow May 9 '16 at 18:03