Representing a number as a matrix

Hi, I'm looking for how i would be able to seperate a number's digits and to then have these stored as a matrix. So for example if the number was 146, this would then be split such that matrix X = [1,4,6] Any help would be greatly appreciated. Thanks

 채택된 답변

James Tursa
James Tursa 2011년 2월 26일

1 개 추천

For a non-negative integral value:
>> x = 146
x =
146
>> num2str(x) - '0'
ans =
1 4 6
Do you also need to somehow process non-integral values, etc?

댓글 수: 1

Alex
Alex 2011년 3월 4일
Thanks. No I don't need to use non-intergrals at the moment, so that should be fine. Thanks again

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt Fig
Matt Fig 2011년 2월 26일

0 개 추천

One approach:
N = arrayfun(@str2num,sprintf('%i',146))
or (along the lines of James' answer)
N = sprintf('%i',146)-'0'

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

질문:

2011년 2월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by