1
$\begingroup$

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}];
$\endgroup$
2
$\begingroup$

{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.

$\endgroup$
1
  • $\begingroup$ @Szaolcs Thanks a lot! $\endgroup$ – Lawerance Jul 26 '14 at 18:21

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.