Clear Filters
Clear Filters

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

1 view (last 30 days)
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.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 22 Nov 2011
n=10;
A=zeros(n);
A(isprime(abs(bsxfun(@minus,(1:n)',1:n))))=1

More Answers (1)

Andrei Bobrov
Andrei Bobrov on 22 Nov 2011
n = 10
out = bsxfun(@(x,y)isprime(abs(x-y)),1:n,(1:n)')

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!