Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given an expression x, are the following two statements interchangable with no exceptions?

  • Depth[x] - 1 == 2

  • x // MatrixQ

If they are, which is evaluated faster? Other than for speed, is there a reason one should be used rather than the other?

share|improve this question
I confess to being mystified by this question. First, what is a "phrase," exactly? Second, if we take these to be Mathematica expressions, it is obvious they do completely different things, leaving me wondering what you might mean by "interchangeable." I am voting to close this as a non-question. – whuber Dec 21 '12 at 17:49
@whuber I think you are being exceptionally hars. While it's true that the answer is most likely not what was expected, it's still a valid question, and if someone was reading through code littered with the first check, which would work correctly some times but fail other times, it's worth asking if they are the same. – jVincent Dec 21 '12 at 20:43
@roger For the purpose of timing expressions you should look at the doc pages for the function Timing – jVincent Dec 21 '12 at 20:47
I appreciate your comment, @jVincent. I would vote to reopen if the O.P. would at least explain what they think each expression is supposed to be doing. Without that, I maintain there is no question here except as each reader individually chooses to imagine it. Your personal construction of the question, as reflected in your answer, is reasonable, but whether it is a unique interpretation or the intended interpretation is difficult to determine. – whuber Dec 21 '12 at 20:58
[2/2] In this case, several folks felt that comparing Depth and MatrixQ fell into the latter category and coupled with the lack of reasoning, it was NaRQ (which is what @whuber was saying above). Note that the ==2 part was added later by Istvan in what is a major change to the question and was not made by the OP (there are no deleted comments by the OP either). So the original closure was fully justified. I've reopened this now, because for good or for bad, Istvan's edit + your interpretation makes a compelling case for a possible source of confusion and thus is a worthy question. – R. M. Dec 22 '12 at 9:03

I think you might be confused about what they both do. While it's true that a matrix with only numerical entries will give True for the first definition, they are not the same:

 {{1, 2}, {3, 4}} // {Depth[#] - 1 == 2, MatrixQ[#]} &

{True, True}

 {Sin[k], {3, 4}} // {Depth[#] - 1 == 2, MatrixQ[#]} &

{True, False}

 {{Sin[y], 2}, {3, 4}} // {Depth[#] - 1 == 2, MatrixQ[#]} &

{False, True}

I can't say for certain what you are using this for, but I'd bet that you want MatrixQ and not the other test.

share|improve this answer

Your Answer


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.