Thread Subject:
Number to vector

Subject: Number to vector

From: mee gestaf

Date: 17 May, 2010 16:12:04

Message: 1 of 8

How can I transform a number ex: 12345 into a vector ex [1 2 3 4 5]

Subject: Number to vector

From: Walter Roberson

Date: 17 May, 2010 16:17:34

Message: 2 of 8

mee gestaf wrote:
> How can I transform a number ex: 12345 into a vector ex [1 2 3 4 5]

textscan(sprintf('%d',TheNumber), '%1d', 'Combine', 1)

Note that is a percent-one-dee, not percent-lowercase-ell-dee .

The result will probably be a cell array containing the vector that you
want.

Subject: Number to vector

From: James Tursa

Date: 17 May, 2010 16:21:20

Message: 3 of 8

"mee gestaf" <metallheart_18@yahoo.com> wrote in message <hsrpsk$nv1$1@fred.mathworks.com>...
> How can I transform a number ex: 12345 into a vector ex [1 2 3 4 5]

>> x = 12345
x =
       12345
>> num2str(x) - '0'
ans =
     1 2 3 4 5

James Tursa

Subject: Number to vector

From: mee gestaf

Date: 17 May, 2010 16:29:07

Message: 4 of 8

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <hsrqe0$60u$1@fred.mathworks.com>...
> "mee gestaf" <metallheart_18@yahoo.com> wrote in message <hsrpsk$nv1$1@fred.mathworks.com>...
> > How can I transform a number ex: 12345 into a vector ex [1 2 3 4 5]
>
> >> x = 12345
> x =
> 12345
> >> num2str(x) - '0'
> ans =
> 1 2 3 4 5
>
> James Tursa


THANKS, THAT IS SIMPLE.

Subject: Number to vector

From: Mark Shore

Date: 17 May, 2010 17:12:05

Message: 5 of 8

"mee gestaf" <metallheart_18@yahoo.com> wrote in message <hsrqsj$a7e$1@fred.mathworks.com>...
> "James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <hsrqe0$60u$1@fred.mathworks.com>...
> > "mee gestaf" <metallheart_18@yahoo.com> wrote in message <hsrpsk$nv1$1@fred.mathworks.com>...
> > > How can I transform a number ex: 12345 into a vector ex [1 2 3 4 5]
> >
> > >> x = 12345
> > x =
> > 12345
> > >> num2str(x) - '0'
> > ans =
> > 1 2 3 4 5
> >
> > James Tursa
>
>
> THANKS, THAT IS SIMPLE.

OK, simple but NOT obvious. It took me a while to see how this trick works, and why '12345' - '0' does not equal '12345' + '0'. A hint, 'b' - 'a' = 'c' - 'b' = 1.

Subject: Number to vector

From: James Tursa

Date: 17 May, 2010 21:46:04

Message: 6 of 8

"Mark Shore" <mshore@magmageosciences.ca> wrote in message <hsrtd5$kb2$1@fred.mathworks.com>...
> "mee gestaf" <metallheart_18@yahoo.com> wrote in message <hsrqsj$a7e$1@fred.mathworks.com>...
> > "James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <hsrqe0$60u$1@fred.mathworks.com>...
> > > "mee gestaf" <metallheart_18@yahoo.com> wrote in message <hsrpsk$nv1$1@fred.mathworks.com>...
> > > > How can I transform a number ex: 12345 into a vector ex [1 2 3 4 5]
> > >
> > > >> x = 12345
> > > x =
> > > 12345
> > > >> num2str(x) - '0'
> > > ans =
> > > 1 2 3 4 5
> > >
> > > James Tursa
> >
> >
> > THANKS, THAT IS SIMPLE.
>
> OK, simple but NOT obvious. It took me a while to see how this trick works, and why '12345' - '0' does not equal '12345' + '0'. A hint, 'b' - 'a' = 'c' - 'b' = 1.

I suppose I should point out that the "trick" works because the character encoding scheme in use has the characters '0', '1', ..., '9' all next to each other and different by 1. e.g., this is true of ASCII and EBCDIC encoding. Also, subtracting character values in MATLAB automatically promotes the result to a double type.

James Tursa

Subject: Number to vector

From: Mark Shore

Date: 17 May, 2010 22:36:04

Message: 7 of 8

>
> I suppose I should point out that the "trick" works because the character encoding scheme in use has the characters '0', '1', ..., '9' all next to each other and different by 1. e.g., this is true of ASCII and EBCDIC encoding. Also, subtracting character values in MATLAB automatically promotes the result to a double type.
>
> James Tursa

Maybe, this being a newsgroup with much potential for miscommunication, I should say that "trick" was not meant in a derogatory sense.

"Ingenious use of relatively cryptic MATLAB functionality", maybe?

Subject: Number to vector

From: James Tursa

Date: 18 May, 2010 00:10:07

Message: 8 of 8

"Mark Shore" <mshore@magmageosciences.ca> wrote in message <hssgcj$9h8$1@fred.mathworks.com>...
> >
> > I suppose I should point out that the "trick" works because the character encoding scheme in use has the characters '0', '1', ..., '9' all next to each other and different by 1. e.g., this is true of ASCII and EBCDIC encoding. Also, subtracting character values in MATLAB automatically promotes the result to a double type.
> >
> > James Tursa
>
> Maybe, this being a newsgroup with much potential for miscommunication, I should say that "trick" was not meant in a derogatory sense.

No offense taken ... I sometimes "post-in-haste" and additional enlightening comments by others never hurt :)

James Tursa

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us