Left digits

15 visualizzazioni (ultimi 30 giorni)
joseph Frank
joseph Frank il 14 Giu 2011
if I have a number such as 1234, how can I select the left 3 digits 123?

Risposta accettata

Sean de Wolski
Sean de Wolski il 14 Giu 2011
left3 = @(x)str2double(x(1:3));
left3(num2str(1234))
Perhaps?
Or so it doesn't error on numbers < 100
left3 = @(x)str2double(x(1:min(3,length(x))));
  1 Commento
Matt Fig
Matt Fig il 14 Giu 2011
num2str(1234)

Accedi per commentare.

Più risposte (2)

Walter Roberson
Walter Roberson il 14 Giu 2011
You can do it arithmetically with something like
floor(x ./ 10.^ceil(log10(x+1)-3))
provided the values are at least 100

Paulo Silva
Paulo Silva il 14 Giu 2011
n=num2str(1234)
n1=str2num(n(1:end-1))

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by