Left digits

2 ビュー (過去 30 日間)
joseph Frank
joseph Frank 2011 年 6 月 14 日
if I have a number such as 1234, how can I select the left 3 digits 123?

採用された回答

Sean de Wolski
Sean de Wolski 2011 年 6 月 14 日
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 件のコメント
Matt Fig
Matt Fig 2011 年 6 月 14 日
num2str(1234)

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

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2011 年 6 月 14 日
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 2011 年 6 月 14 日
n=num2str(1234)
n1=str2num(n(1:end-1))

カテゴリ

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