Store prime and non-prime numbers in two diferent vectors from a .txt
1 view (last 30 days)
Show older comments
ABG on 29 Sep 2022
Commented: ABG on 30 Sep 2022
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!
load('Natural_numbers.txt'); %loading the data
x=x'; % i want the data in a row
prime_num=[x, i] ;
Eric Delgado on 29 Sep 2022
% x = load('Natural_numbers.txt');
x = [1, 11, 21, 23, 25, 27, 29, 30, 31, 44];
idx = isprime(x);
Prime = x(idx)
nonPrime = x(~idx)
% writematrix(Prime, "Prime.txt")
% writematrix(nonPrime, "nonPrime.txt")
More Answers (1)
David Hill on 29 Sep 2022
Edited: David Hill on 29 Sep 2022
Attach Natural_numbers.txt is below does not work
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!