views:

227

answers:

1

This is a follow-up question to another SO question of mine.

I have a large array of 8 bit binary values that I want to convert back to uint8.

I have used Amro's lookupTable solution from the previous question. Now I want to do the reverse. I wanted to do a lookup table but sadly I am unable to.

What I managed to do is the following:

temp = ones([(TotalPixel),1], 'uint8');

for iter2 = 1 : TotalPixel,
     temp(iter2,1) = sum(Data(iter2,1:8).*2.^(7:-1:0));   
end

But the for loop is too slow as it takes 2 seconds to convert a [76800 x 1] array. Is there a better/faster way to do this?

+3  A: 

Try this:

temp = uint8(Data*(2.^(7:-1:0))');

Note that this answer is basically the same as an edit I made to an answer I gave to a previous question you asked. You had asked there about just converting a single row of 12-bit values, but I had added an extra discussion of how you would extend it to converting multiple values at once using a matrix multiply. The difference between that answer and this is simply the number of bits and the inclusion of UINT8 to change the variable type.

gnovice
Thanks for all the help!
HH