From: Erik on
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
From: ImageAnalyst on
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.
From: Erik on
Many thanks, brain's a little blurry right now.

(For those wondering, num = sum(vec.*10.^[4 3 2 1 0]) )
From: Matt Fig on
"Erik" <emiehling(a)gmail.com> wrote in message <hjb3j7$pgl$1(a)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))
From: Matt J on

Another possibility:

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