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

5 Ansichten (letzte 30 Tage)
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 Kommentare
Rania Fahmy
Rania Fahmy am 14 Dez. 2018
Bearbeitet: Rania Fahmy am 14 Dez. 2018
they do have to be consecutive yes
Fangjun Jiang
Fangjun Jiang am 14 Dez. 2018
I can suggest utilizing sparse matrix, see sparse().

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume
Guillaume am 14 Dez. 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'

Weitere Antworten (1)

Omer Yasin Birey
Omer Yasin Birey am 14 Dez. 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 Kommentare
Guillaume
Guillaume am 14 Dez. 2018
Bearbeitet: Guillaume am 14 Dez. 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.
Rania Fahmy
Rania Fahmy am 14 Dez. 2018
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Identification finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by