How to simulate bit stuffing on a 20860×1 matrix of binary data?
5 views (last 30 days)
Show older comments
Hello everyone,
i have a matrix of binary data of size 20860×1. (20860 rows and 1 column), and i would like to perform bit stuffing on it, which means to insert a zero after each six consecutive ones
so if i have a sequence like this in my matrix: 0 0 0 1 1 1 1 1 1 1 0
then after the bit stuffing it will be like this: 0 0 0 1 1 1 1 1 1 0 1 0
i tried doing this using two for loops but it didn't work properly
let the data matrix be called: collect, and let it be a matrix of random binary data
collect=randi([1 0],20860,1);
3 Comments
Accepted Answer
Guillaume
on 14 Dec 2018
An easy way:
%demo array that will fail with most naive algorithms:
v = [ones(1, 8), 0, ones(1, 5), 0, ones(1, 20)]
newv = regexprep(char(v + '0'), '111111', '1111110') - '0'
More Answers (1)
Omer Yasin Birey
on 14 Dec 2018
Hi Rania, you may try this:
a =randi([0 1],1,20860);
a = num2str(a);
a = strsplit(a);
a = strcat(a);
a = cell2mat(a);
k = strfind(a,'111111');
for i = 1:length(k)
b = [a(1:k(i)+6) '0' a(k(i)+8:end)];
end
b = b';
2 Comments
Guillaume
on 14 Dec 2018
Edited: Guillaume
on 14 Dec 2018
I've not tried to fully understand what this answer is doing. I'll note that conversion to string (and back) is going to be slow.
At the very least, for a vector of 0 and 1, a conversion to string with:
a = randi([0 1], 1, 20860);
str = char(a + '0');
is going to be much faster than num2str.
Note that although undocumented strfind also works with vectors of numbers.
Also, I suspect that this answer will insert a 0 after the 6th, 7th and 8th one in a sequence of 8 consecutive 1.
edit: actually, it would do if the loop wasn't completely broken. As it is, it just insert a 0 at the end of the last sequence no matter how many there are.
See Also
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!