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

 Réponse acceptée

James Tursa
James Tursa le 26 Fév 2011

1 vote

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 commentaire

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

Connectez-vous pour commenter.

Plus de réponses (1)

Matt Fig
Matt Fig le 26 Fév 2011

0 votes

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

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by