11
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Thanks for the various answers, I'm not yet confortable with these syntax. $\endgroup$ – Cedric H. Aug 28 '14 at 11:00
16
$\begingroup$

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]
$\endgroup$
9
$\begingroup$
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.

$\endgroup$
6
$\begingroup$
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

$\endgroup$
  • 1
    $\begingroup$ Nice comparison, but why not VectorQ, e.g. (51385)? $\endgroup$ – Mr.Wizard Aug 28 '14 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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