multiple digit number in to individual digits
12 ビュー (過去 30 日間)
古いコメントを表示
i want to change the number 1123 in 1 1 2 3, want to split combine number into into individual numbers
0 件のコメント
採用された回答
Azzi Abdelmalek
2014 年 7 月 22 日
a=1234
b=str2double(regexp(num2str(a),'\d','match'))
3 件のコメント
Walter Roberson
2017 年 2 月 7 日
a = '123*+'
for K = 1 : length(a)
fprintf('character #%d of "%s" is "%c"\n', K, a, a(K));
end
Adam Danz
2020 年 4 月 29 日
For large values such as a=11122333345555566 this will not work since num2str will convert the value to '1.112233334555557e+16'. Otherwise nice solution.
その他の回答 (3 件)
John D'Errico
2014 年 7 月 22 日
N = 1123;
Ndigits = dec2base(N,10) - '0'
Ndigits =
1 1 2 3
3 件のコメント
John D'Errico
2023 年 2 月 27 日
編集済み: John D'Errico
2023 年 2 月 27 日
Not difficult with a floating point number, but remember that a float is NOT an exact decimal representation of that number. But...
X = 1.2345;
dec2base(X*10000,10)
or
dec2base(X*10000,10) - '0'
You can even fuss around and get the decimal point in there if you want, but if you want that, then sprintf is arguably a better choice.
Jan
2017 年 2 月 7 日
編集済み: Jan
2017 年 2 月 14 日
For getting the digits, a conversion to a string is an indirection. Staying at numerical values is usually faster:
N = 1123;
m = floor(log10(N)); % [EDITED] Thanks Stephen
D = mod(floor(N ./ 10 .^ (m:-1:0)), 10);
Ramon Villamangca
2018 年 11 月 20 日
編集済み: Ramon Villamangca
2018 年 11 月 20 日
a simple single line solution:
>> num = 12345042117;
>> arrayfun(@(x) mod(floor(num/10^x),10),floor(log10(num)):-1:0)
ans =
1 2 3 4 5 0 4 2 1 1 7
3 件のコメント
Ramon Villamangca
2023 年 2 月 27 日
@Jyahway Dong: If the digits are that long, you'll probably input it as a char string, anyway. That means the solution would even be much simpler:
num = '62229893423380308135336276614282806444486645238749';
num - '0'
参考
カテゴリ
Help Center および File Exchange で Calendar についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!