Borrar filtros
Borrar filtros

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

1 visualización (últimos 30 días)
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.

Respuesta aceptada

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

Más respuestas (1)

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

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by