When you read the primes documentation it describes that the function "returns a row vector containing all the prime numbers less than or equal to n." That means on each iteration of your loop y is a vector containing all primes between 1 and x. What do you want to do with all of them? Your code does nothing to handle that whole vector, and you don't make any attempt to remove the prime numbers less than num1. You also never use indexing inside the loop, so all of those y vectors are discarded except for the last one. There does not seem to be much point to that.
I think isprime would be more useful for you, because you can then use its output to index into your vector, thus giving you all prime numbers between the two numbers: num1 = str2double(input('Enter Number 1: ','s'));
num2 = str2double(input('Enter Number 2: ','s'));
v = num1:num2;
y = v(isprime(v));
fprintf('The primes between %d and %d are: \n',num1, num2);
disp(y);
and tested:
Enter Number 1: 15
Enter Number 2: 30
The primes between 15 and 30 are:
17 19 23 29
Or you could use primes and remove the lower part using indexing:
v = primes(num2);
y = v(v>=num1)
Best Answer