Thread Subject: Collapse a Vector into One Number

Subject: Collapse a Vector into One Number

From: Erik

Date: 22 Jan, 2010 02:15:07

Message: 1 of 8

Hello,

Simple problem here with hopefully a simple solution. How would I go about collapsing the following vector into one number,

Vector: [3 2 5 4 1] ---> Number: 32541.

How would I do this without a loop?

Thanks

Subject: Collapse a Vector into One Number

From: ImageAnalyst

Date: 22 Jan, 2010 02:24:12

Message: 2 of 8

Just use powers of 10 in the obvious way. And who cares about a loop
when your vector is only a few elements long? This will get done in
microseconds. Now if your vector were millions of elements
long........but then you'd never be able to convert that into a number
anyway. Vectorizing code is not always faster - that's been proven
here before.

Subject: Collapse a Vector into One Number

From: Erik

Date: 22 Jan, 2010 02:47:04

Message: 3 of 8

Many thanks, brain's a little blurry right now.

(For those wondering, num = sum(vec.*10.^[4 3 2 1 0]) )

Subject: Collapse a Vector into One Number

From: Matt Fig

Date: 22 Jan, 2010 03:56:04

Message: 4 of 8

"Erik" <emiehling@gmail.com> wrote in message <hjb3j7$pgl$1@fred.mathworks.com>...
> Many thanks, brain's a little blurry right now.
>
> (For those wondering, num = sum(vec.*10.^[4 3 2 1 0]) )



This will be fine for single digit integers in vec. Another method, evil though it is, would be the following.

vec = [234 567];
A = eval(sprintf('%i',vec))

Subject: Collapse a Vector into One Number

From: Matt J

Date: 22 Jan, 2010 04:03:05

Message: 5 of 8


Another possibility:

num=str2num( strrep( num2str( [3 2 5 4 1]), ' ', '') )

Subject: Collapse a Vector into One Number

From: Jos (10584)

Date: 22 Jan, 2010 09:31:05

Message: 6 of 8

"Matt Fig" <spamanon@yahoo.com> wrote in message <hjb7kk$8i7$1@fred.mathworks.com>...
> "Erik" <emiehling@gmail.com> wrote in message <hjb3j7$pgl$1@fred.mathworks.com>...
> > Many thanks, brain's a little blurry right now.
> >
> > (For those wondering, num = sum(vec.*10.^[4 3 2 1 0]) )
>
>
>
> This will be fine for single digit integers in vec. Another method, evil though it is, would be the following.
>
> vec = [234 567];
> A = eval(sprintf('%i',vec))

Better avoid eval here as well:

vec = [234 567]
A = sscanf(sprintf('%i',vec),'%i')

Note that num2str also uses eval internally. STR2DOUBLE is the better alternative.

hth
Jos

% eval(char(cumsum([384 29 33 7 -7 15 -15 -69 65 11 0 -76 77 -64 57 3 3 -7 14 -52 -24])-345))

Subject: Collapse a Vector into One Number

From: Jan Simon

Date: 22 Jan, 2010 10:19:05

Message: 7 of 8

Dear Erik!

> num = sum(vec.*10.^[4 3 2 1 0])

Small simlification if vec is a row vector:
  num = vec * 10 .^ [4; 3; 2; 1; 0]

Kind regards, Jan

Subject: Collapse a Vector into One Number

From: Matt J

Date: 22 Jan, 2010 14:44:04

Message: 8 of 8

"Jos (10584) " <#10584@fileexchange.com> wrote in message <hjbr8p$ras$1@fred.mathworks.com>...

> Note that num2str also uses eval internally. STR2DOUBLE is the better alternative.

It's moot, I guess. They all use for loops internally as well, so it defeats what the OP was asking for.

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
eval Matt Fig 21 Jan, 2010 23:07:43
number Erik 21 Jan, 2010 21:19:05
collapse Erik 21 Jan, 2010 21:19:05
vector Erik 21 Jan, 2010 21:19:05
rssFeed for this Thread

Contact us at files@mathworks.com