How to access each digit of a number in matlab without converting it into string?
24 次查看(过去 30 天)
显示 更早的评论
How to access each digit of a number in matlab?
0 个评论
回答(2 个)
Stephen23
2015-7-11
编辑:Stephen23
2015-7-11
>> A = 54321;
>> rem(floor(A./(10.^(floor(log10(A)):-1:0))),10)
ans =
5 4 3 2 1
>> A = 918287465;
>> rem(floor(A./(10.^(floor(log10(A)):-1:0))),10)
ans =
9 1 8 2 8 7 4 6 5
But converting to a string first will be faster and simpler:
>> A = 918287465;
>> int2str(A)-'0'
ans =
9 1 8 2 8 7 4 6 5
0 个评论
Ashmil Mohammed
2015-7-11
You may use a foor loop and keep on dividing the number with 10.Use the remainder(modulo operator).
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!