Changing prime numbers in a matrix
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I printed a 100x100 random matrix of integers from 1 to 50. However, I want to replace the prime numbers in this matrix with the number 1415. How can I replace the prime numbers in that matrix with a specific number (1415)? Thank you.
0 comentarios
Respuestas (1)
Scott MacKenzie
el 16 de Jun. de 2021
M = randi([1 50],100);
M(isprime(M)) = 1415;
2 comentarios
Scott MacKenzie
el 16 de Jun. de 2021
Ok, sure. This modification stores the locations of the prime numbers in a logical variable, outputs the prime numbers, then replaces the prime numbers in M with 1415.
M = randi([1 50],100);
% find locations in M containing prime number
primeLogical = isprime(M);
% output prime numbers in M
M(primeLogical)
% change prime numbers in M to 1415
M(primeLogical) = 1415;
Ver también
Categorías
Más información sobre Matrix Indexing 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!