Sorting Prime Numbers From an Array of Numbers, Into a New Array

7 vues (au cours des 30 derniers jours)
Kevin Krone
Kevin Krone le 20 Sep 2019
Commenté : David Hill le 20 Sep 2019
n = [1:1:100];
N = isprime(n);
NN=[];
for i = 1:1:100
if N(i)==1 %if N at a position is equal to 1, then it is a prime number
n(i) %Displays the integer at that position %Now that number needs to be stored in a new array
NN=n(i) %This just sets the new array equal to the prime number n(i), this doesn't store it. It is replaced on the next loop
end
end
I need to know how to make a new array filled with the prime numbers from the loop

Réponse acceptée

David Hill
David Hill le 20 Sep 2019
Why not use the primes function?
NN=primes(100);
  2 commentaires
madhan ravi
madhan ravi le 20 Sep 2019
Homework probably.
David Hill
David Hill le 20 Sep 2019
NN=n(ismember(n,primes(max(n))));
For generic array n.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by