MATLAB: Manipulate Matrix Element using subscripts (ex: isprime(i-j)) or (i+j)>10)

matrix element manipulation select by subscript formula value

I need to create a oneline command to set a 'n x n' matrix to {1 if i-j isprime, 0 if not). I don't remember how to select an element in this manor. Thank you.

Best Answer

n=10;
A=zeros(n);
A(isprime(abs(bsxfun(@minus,(1:n)',1:n))))=1