I'm having trouble suppressing duplicate numbers. Can someone please help me to fix this within my while loop?

조회 수: 3 (최근 30일)
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]

채택된 답변

Image Analyst
Image Analyst 2015년 5월 1일
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
  댓글 수: 2
Tank
Tank 2015년 5월 1일
편집: Tank 2015년 5월 1일
Okay, I see your reasoning behind this much simpler code. But I'd like it to only display the array at the very end, how would I be able to check for uniqueness within the loop besides the ismember function? I know it's more complicated, but is it that much more work?
Image Analyst
Image Analyst 2015년 5월 1일
Tank, just pull out the fprintf() and move them to the end:
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]);
else
fprintf('Sorry, input must be between 10 & 100.\nTry again.')
end
end
fprintf('At the end, M = [', userNumber);
fprintf('%.1f ', M);
fprintf(']\n');

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

pfb
pfb 2015년 5월 1일
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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by