MATLAB: I’m having trouble suppressing duplicate numbers. Can someone please help me to fix this within the while loop

uniquewhile loop

This code needs to accept 5 input numbers between 10 and 100 inclusive, then display each value as they're entered, but only if it's not a duplicate of a previously entered number. Also, it needs to display all unique numbers in a 1-D vector after all 5 values have been entered.
The 1-D vector at the end is working perfectly fine, but the numbers are still displaying at the beginning as they're entered. All I need is to be able to suppress the non-unique numbers as they're entered.
Here's my code:
clear, clc
M=[];
M1=[];
V=input('Enter a number between and including 10 and 100: ');
if V>=10 && V<=100
disp(V)
M(1)=V;
end
W=[];
I=2;
J=1;
K=2;
M(1)=V(1);
while I<=5
W=input('Enter a number between and including 10 and 100: ');
V(I)=W;
if V(I)>=10 && V(I)<=100
disp(W)
M(I)= W;
else
disp('Sorry, input must be between 10 & 100. Start Over')
return
end
for K=1:I
if V(I)==W(K)
break
end
end
if (K==I)
disp(V(I))
M(J)= V(I);
J=J+1;
end
I=I+1;
end
M=sort(M);
for J=2:5
if M(J)~=M(J-1)
M1=[M1 M(J)];
end
end
array=[M(1) M1]

Best Answer

Way too complicated. Simply call unique() and don't even bother checking if it has been entered before or not:
clc;
M=[];
while length(M) < 5
userNumber=input('Enter a number between and including 10 and 100 (or 0 to quit): ');
if userNumber == 0
break; % user wants to quit.
end
if userNumber >= 10 && userNumber <= 100
M = unique([M, userNumber]);
fprintf('Good! You entered %.1f so now M = [', userNumber);
fprintf('%.1f ', M);
fprintf(']\n');
else
fprintf('Sorry, input must be between 10 & 100.\nTry again.')
end
end