4
$\begingroup$

I have this test implemented in my code, when I import a numeric matrix, which I thought would be bulletproof, but it's failing in this case:

In[]:= MatrixQ[{{}, {}}, NumericQ[#] && MatchQ[#, _Real] &]

Out[]= True

I don't get it.

$\endgroup$

1 Answer 1

6
$\begingroup$

Because there are no elements in your matrix. So there is no element which is not numeric and real.

MatrixQ[{{}, {}}, False &]

returns True too.

So you have to exclude that case:

MatrixQ[##, NumericQ[#] && MatchQ[#, _Real] &] && !MatrixQ[##, False &] &
$\endgroup$
3
  • 1
    $\begingroup$ I'd find the example MatrixQ[{{}, {}}, False &] somewhat more convincing. (Typo? If not, consider MatrixQ[{{1}, {1}}, True].) Or maybe MatrixQ[{{}, {}}, Print] vs. MatrixQ[{{1}, {1}}, Print]. In any case, +1. $\endgroup$
    – Michael E2
    Oct 15, 2016 at 21:40
  • $\begingroup$ @MichaelE2 Whoops. Thanks for pointing out $\endgroup$
    – meneken17
    Oct 15, 2016 at 21:43
  • $\begingroup$ Thanks! And how can I catch this case? I have tried several things without success. $\endgroup$
    – Santiago
    Oct 16, 2016 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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