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

1 次查看(过去 30 天)
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.

采纳的回答

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

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by