how to adding zero of specific position into binary sequence?

1 view (last 30 days)
i have binary sequence
A= [ 1 1 1 1 1 0 0 1 0 1 1 1 1 1 1 0 0 1] >>> 18 bits.
and wants to add zero (specific position)to binary sequence after every 4th bit of A so the output will be
B=[1 1 1 1 0 1 0 0 1 0 0 1 1 1 0 1 1 1 0 0 0 1]>>> 22 bit.
how to do this using matlab ? is there any syntax for it? also, i want to get back the first binary sequence of A from B which removes the zeros from B to get A again?
pls, help me to do it.
thanks.

Answers (1)

Image Analyst
Image Analyst on 14 Oct 2017
Sounds like homework, so I'm only giving a partial solution. But all you have to do is to figure out how to crop B to the right length:
A= [ 1 1 1 1 1 0 0 1 0 1 1 1 1 1 1 0 0 1]
% Make sure A is a multiple of 4
lenA = length(A)
newLength = ceil(lenA/4) * 4
% Pad to end with 0
A(newLength) = 0;
% Make into matrix.
a2 = reshape(A, 4, [])
% Tack on row of zeros
[rows, columns] = size(a2);
a2(rows+1, columns) = 0
% Reshape back to 1-d
B = a2(:)'
% Now crop off some B if needed. Hint: use rem().

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!