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

2 次查看(过去 30 天)
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