Mode in matlab doesn't work.. can anybody help me with this....?

조회 수: 2 (최근 30일)
Glenn Macion
Glenn Macion 2015년 2월 3일
댓글: John D'Errico 2015년 2월 26일
this is my code.. and i'm having a problem running mode for the array i made...
while (1)
userNumber=input('\nPlease enter number:', 's');
if(isempty(userNumber))
break;
end
[myNumber, myStatus] = str2num(userNumber);
if(myStatus == 0 || myNumber < -999999 || myNumber > 999999)
continue;
end
end
fprintf('\n\t\tThe mode of the given set of numbers is:');
x=str2num(myNumber);
MODE=mode(x)
pause;
clc;

채택된 답변

Stephen23
Stephen23 2015년 2월 3일
편집: Stephen23 2015년 2월 3일
MATLAB's mode works just fine on my computer. Your code was not actually storing any of the values in each loop, and there is also a bit of confusion between string and numeric values. Try this instead:
userNumber = 0;
userVec = [];
while ~isempty(userNumber)
userNumber = sscanf(input('Please enter an integer: ','s'),'%d');
userVec = [userVec,userNumber]; %#ok<AGROW>
end
userVec = userVec(-1e6<userVec & userVec<1e6);
fprintf('The mode of the given set of numbers is %d\n', mode(userVec))
  댓글 수: 2
Glenn Macion
Glenn Macion 2015년 2월 25일
thank you sir... this helps me a lot.... no wonder it didn't work. it did not store the values at all. thank you for your answer. =D
John D'Errico
John D'Errico 2015년 2월 26일
But more than that, as Stephen points out, you cannot take the mode of a string, and have it mean something useful in terms of numbers in that string. For example,
mode('antidisestablishmentarianism')
ans =
i
The mode is the letter i. So I suppose if you are looking for the most common digit in the decimal representation of a number, mode would help there.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by