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

2 Ansichten (letzte 30 Tage)
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;

Akzeptierte Antwort

Stephen23
Stephen23 am 3 Feb. 2015
Bearbeitet: Stephen23 am 3 Feb. 2015
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 Kommentare
Glenn Macion
Glenn Macion am 25 Feb. 2015
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 am 26 Feb. 2015
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by