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.

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

1 Answer 1

up vote 1 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 at 18:21

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.