MATLAB: Array of N prime numbers

for loopsif statementprime numberwhile loop

Hello. I'm new to MatLab. I've just written a code to identify if a given number N is a prime number.
Basically, that's it:
function [out] = myIsPrime(n)
if n == 1
out = 0;
elseif n == 2
out = 1;
elseif all(rem(n,2:n-1) ~= 0)
out = 1;
else
out = 0;
end
end
Now I want my output to be the first 'N' prime numbers counting from 2 and up.
I've been trying for hours but my brain is just frozen. How do I do that using if, for or while loops?

Best Answer

while the count of primes found is less than your target count, keep trying the next number in sequence to determine whether it is prime; if it is then increment the count of primes found and store the prime in your array.