How to access each digit of a number in matlab without converting it into string?

조회 수: 7 (최근 30일)
Anushka
Anushka 2015년 7월 11일
편집: Stephen23 2015년 7월 11일
How to access each digit of a number in matlab?

답변 (2개)

Stephen23
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

Ashmil Mohammed
Ashmil Mohammed 2015년 7월 11일
You may use a foor loop and keep on dividing the number with 10.Use the remainder(modulo operator).

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by