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;
  1 件のコメント
Andreas Goser
Andreas Goser 2015 年 2 月 3 日
Can you share what the problem is?

サインインしてコメントする。

採用された回答

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 Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by