Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a matrix a n columns and m lines. I'd like to efficiently remove the lines that are not entirely composed of numeric values, lines like this:

{6.22926,6.23254,6.23128,,6.23453,6.22548,6.22938,6.23443,}

In this case the last element is not a numeric value, and such line should be removed.

Also, the matrix is quite big, I have no clue what an efficient method would be.

share|improve this question
    
Thanks for the various answers, I'm not yet confortable with these syntax. –  Cedric H. Aug 28 at 11:00

3 Answers 3

up vote 10 down vote accepted

The following should work:

lis = {{1, 2, 3, 4, 5, 6}, {1, 9, , 4, 6, 2}, {4, a, 3, 7, 1, 2}, 
      {3.4, 5.2, 6.5, 7.7, 6.1, 2}};

Then:

Cases[lis, {__?NumericQ}]
{{1, 2, 3, 4, 5, 6}, {3.4, 5.2, 6.5, 7.7, 6.1, 2}}

We can also use VectorQ with Select

Select[lis, VectorQ[#, NumericQ] &]

If you have Version 10, the following works:

Select[lis, AllTrue @ NumericQ]
share|improve this answer
m = {{6.22926, 6.23254, 6.23128, 0, 6.23453, 6.22548, 6.22938, 
   6.23443, 0}, {6.22926, 6.23254, 6.23128, , 6.23453, 6.22548, 
   6.22938, 6.23443,}}

Select[m, MatrixQ[{#}, NumericQ] &]
{{6.22926, 6.23254, 6.23128, 0, 6.23453, 6.22548, 6.22938, 6.23443, 0}}

more alternatives for speed comparisons.

share|improve this answer
    
+1 also, faster than replacement. –  Chris Degnen Aug 28 at 11:56
m = {{1, 2, 3}, {, 2, 3}, {1.1, 2, 3}, {1.1, , 3}}

DeleteCases[m, 
 Alternatives @@ Select[m, MemberQ[#, _?(Not[NumericQ[#]] &)] &]]

{{1, 2, 3}, {1.1, 2, 3}}

Edit

A timing comparison. First creating a matrix:-

m = RandomReal[1, {10000, 10000}];

(* Adding some blanks *)
(m[[Sequence @@ #]] = "") & /@ RandomInteger[{1, 10000}, {2000, 2}];

Comparing the various solutions:-

First@Timing[a = Cases[m, {__?NumericQ}]]

19.141323

First@Timing[b = Select[m, MatrixQ[{#}, NumericQ] &]]

0.468003

First@Timing[
  c = DeleteCases[m, 
    Alternatives @@ Select[m, MemberQ[#, _?(Not[NumericQ[#]] &)] &]]]

85.862950

a == b == c

True

share|improve this answer
1  
Nice comparison, but why not VectorQ, e.g. (51385)? –  Mr.Wizard Aug 28 at 12:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.