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

2 visualizaciones (últimos 30 días)
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;

Respuesta aceptada

Stephen23
Stephen23 el 3 de Feb. de 2015
Editada: Stephen23 el 3 de Feb. de 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 comentarios
Glenn Macion
Glenn Macion el 25 de Feb. de 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 el 26 de Feb. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by