MATLAB: How can i combine several matrix elements in one

matrixmatrix operations

Hello!
I have a matrix A with a bits (just an one-dimensional array with one and zeros). Using this matrix A i want to create another matrix B, where our single-digit elements are combined in multi-digit elements.
I mean that i have:
A = [0 1 0 1 0 1 1 0 0 1 1 1]
And as a result i want to receive something like this
B = [0101 0110 0111]
Thanks a lot for your future answers!

Best Answer

The bigger question is why do you want to do this. I can't think of a single scenario where the representation you're asking would be useful.
In any case, it's not possible. A matrix in matlab is displayed as decimal numbers not binary. Furthermore, leading 0s are always omitted (since there's an infinity), so the closest you could do would be:
B = [101 110 111];
which would contain the decimal numbers 100 (binary representation: 1100100), 110 (binary representation: 1101110), and 111 (binary representation: 110111). If that's what you want, then:
A = [0 1 0 1 0 1 1 0 0 1 1 1]
B = sum(reshape(A, 4, []) .* 10.^(3:-1:0)') %in R2016b only
But as said, I don't see how that could be useful for anything. You certainly can't easily do any binary math on this, since the numbers are decimal.
Another option would be to convert to a string, which would allow the leading 0 (as a character)
A = [0 1 0 1 0 1 1 0 0 1 1 1]
B = cellstr(reshape(char(A + '0'), 4, []).').'