MATLAB: I have an array of length 256. It has either 0’s and 1’s of type uint8. I would like to concatenate it so that it occupies less memory and then recover the original array back. please check the eaxample

MATLAB

x = [1 0 1 1 1 0 0 1………..1] % uint8. length = 256. size = 256*8 = 2048 bytes
xbin = 1011100………..1 % binary form. 256 bits or 32 bytes
% from 'xbin', I need to recover 'x' and store it in variable 'y' in 'uint8' format
y = [1 0 1 1 1 0 0 1] % uint8. length = 256. size = 256*8 = 2048 bytes

Best Answer

You're dealing with 2048 bytes and you're worried about memory???? Oh well, anyway...
Try this:
% Generate sample 256 long array of 1's and 0's.
x = randi(2, 1, 256)-1
% Convert to a string.
charx = dec2bin(x)'
% Extract every group of 8 characters into a uint8 number.
for k = 1 : length(charx)/8
numbers(k) = uint8(bin2dec(charx(1+(k-1)*8 : k*8)));
end
% Print to command window.
numbers