MATLAB: How to convert 1D binary data matrix to 2D image matrix

1d to 2d conversionImage Processing Toolboxmatrix arraymatrix manipulation

I have a one-dimensional matrix of size 256X256X8. It has a string of 0 and 1 of the said length. I want to form an 2D Image matrix of size 256×256 from the previous matrix with each eight bits in the original matrix representing an integer in the final matrix. Is there any way to do it?

Best Answer

I think you have to scoot along in chunks of 8 extracting the binary bits, then use bin2dec(), something like (untested)
count = 1;
for index = 1 : 8 : 256*256*8
substring = initialString(index:index+7);
vector1D(count) = bin2dec(substring);
count = count+1;
end
and then you will have a 1D decimal vector of length 256*256.
Then you have to use reshape() to form it into a 2D matrix:
matrix2D = reshape(vector1D, [256, 256]);