# Store prime and non-prime numbers in two diferent vectors from a .txt

1 visualización (últimos 30 días)
ABG el 29 de Sept. de 2022
Comentada: ABG el 30 de Sept. de 2022
Hi guys!
I am really new at programming with matlab, and I am stuck with one problem.
I need to read a bunch of numbers from a .txt file, determine whether this numbers are prime or not and finally store them in two diferent files.
With the code attached, what happens is that all the values from the .txt are stored in the nonprime_num vector, and I don't know why (I have tried many ways but none of them have worked).
If you could help me with this, I would be really grateful! Thank you very much!
x=Natural_numbers;
x=x'; % i want the data in a row
i=2;
prime=1;
n=length(x);
prime_num=[];
nonprime_num=[];
while i<=sqrt(x)
if rem(x,i)==0
prime=0;
break
end
i=i+1;
end
if prime==1
nonprime_num=[x, i];
else
prime_num=[x, i] ;
end
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Eric Delgado el 29 de Sept. de 2022
x = [1, 11, 21, 23, 25, 27, 29, 30, 31, 44];
idx = isprime(x);
Prime = x(idx)
Prime = 1×4
11 23 29 31
nonPrime = x(~idx)
nonPrime = 1×6
1 21 25 27 30 44
% writematrix(Prime, "Prime.txt")
% writematrix(nonPrime, "nonPrime.txt")
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
ABG el 30 de Sept. de 2022
Thank you so much Eric!

Iniciar sesión para comentar.

### Más respuestas (1)

David Hill el 29 de Sept. de 2022
Editada: David Hill el 29 de Sept. de 2022
Attach Natural_numbers.txt is below does not work
idx=isprime(m);
nonprime_num=m(~idx);
prime_num=m(idx);
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
ABG el 30 de Sept. de 2022

Iniciar sesión para comentar.

### Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by