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.


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 &] &
  • 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 '16 at 21:40
  • $\begingroup$ @MichaelE2 Whoops. Thanks for pointing out $\endgroup$ – meneken17 Oct 15 '16 at 21:43
  • $\begingroup$ Thanks! And how can I catch this case? I have tried several things without success. $\endgroup$ – Santiago Oct 16 '16 at 9:18

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.