Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
1  
@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
3  
[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. –  rm -rf Dec 22 '12 at 9:03
show 3 more comments

1 Answer

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
add comment

Your Answer

 
discard

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.