How to remove a line of a matrix if not all elements are numeric?
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]
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.
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