[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: quick way to singles array to byte array
From: |
Jaroslav Hajek |
Subject: |
Re: quick way to singles array to byte array |
Date: |
Thu, 29 Jul 2010 08:13:10 +0200 |
On Thu, Jul 29, 2010 at 7:48 AM, George Nychis <address@hidden> wrote:
> Hi all,
>
> Let's say I have an array of singles, like:
> a=[0.923834,0.8234234,0.234234,0.23414]
>
> If I want to convert this to a byte array, where each number is represented
> by 4 bytes, I've been doing:
> b=[];
> for i=1:length(a)/2
>
> b=[b;typecast(single(a((i-1)*2+1)),'uint8');typecast(single(a((i-1)*2+2)),'uint8')]
> end
>
> Is there an easier, less complex way of doing this?
>
> Thanks!
> George
>
Why don't you just use typecast on the whole array?
b = typecast (single (a));
then manipulate it to get 4 bytes per row:
b = reshape (b, 4, []).';
hth
--
RNDr. Jaroslav Hajek, PhD
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz