How to simulate bit stuffing on a 20860×1 matrix of binary data?

3 次查看(过去 30 天)
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);

采纳的回答

Guillaume
Guillaume 2018-12-14
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'

更多回答(1 个)

Omer Yasin Birey
Omer Yasin Birey 2018-12-14
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 个评论
Guillaume
Guillaume 2018-12-14
编辑:Guillaume 2018-12-14
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.
Rania Fahmy
Rania Fahmy 2018-12-14
I just tried this, and it gives me the same original matrix. For some reason it just didn't add any zeros after the occurance of 6 consecutive ones.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by