MATLAB: How to change values of a matrix when subscribts meet a condition

conditionindexingMATLABmatrixsubscripts

Hi everyone,
I am new to Matlab and I am trying to gain some first experiences using the book 'Learning MATLAB' by T.Discroll. Currently I am struggling with the following problem:
Create a 10 x 10 matrix (A) and replace entries in the following manner:
Set the entry a(i,j) of the matrix A to 1 if i-j is prime, otherwise set the value to zero.
Attempts I had taken were either including the 'ind2sub' command or an if-loop, but that didn't help.
To generalize the question: 'How can i change values of a matrix using subscript indexing, when the subscripts meet a certain condition'.
So far extended search in books or google didn't help either.
Thanks in advance!

Best Answer

One approach:
M = rand(10);
for k1 = 1:numel(M)
[r,c] = ind2sub(size(M), k1);
M(r,c) = 0;
M(r,c) = 1*isprime(abs(r-c));
end