From: Erik on 21 Jan 2010 21:15 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 21 Jan 2010 21:24 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 21 Jan 2010 21:47 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 21 Jan 2010 22:56 "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 21 Jan 2010 23:03 Another possibility: num=str2num( strrep( num2str( [3 2 5 4 1]), ' ', '') )
|
Next
|
Last
Pages: 1 2 Prev: how to optimize the "for" loop execution Next: Alternatives to find function |