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

I got confused by {i_, j_} /; in the following code. I didn't see an explanation in the SparseArray documentation. Can anyone tell me how this part work in the code?

m1 = SparseArray[{{i_, i_} -> -2, {i_, j_} /; Abs[i - j] == 1 -> 1}, {5, 5}];
share|improve this question
up vote 2 down vote accepted

{i_, j_} /; Abs[i - j] == 1 is a pattern. It matches pairs of expressions the difference of which is 1.

/; is Condition.

In SparseArray this will put a 1 on the second diagonals, as the difference of the indices of elements on the second diagonals is 1 or -1.

share|improve this answer
@Szaolcs Thanks a lot! – Lawerance Jul 26 '14 at 18:21

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.