I'm having trouble suppressing duplicate numbers. Can someone please help me to fix this within my while loop?
Show older comments
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]
Accepted Answer
More Answers (1)
pfb
on 1 May 2015
M=zeros(1,5);
for i = 1:5
done=0;
while ~done
V=input('Enter a number between and including 10 and 100: ');
done = (V>=10) & (V<=100) & (~ismember(V,M));
if (~done)
fprintf('sorry the number must be between 10 and 100 and not previously entered. Start over\n')
end
end
M(i)=V;
end
Categories
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!