フィルターのクリア

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

3 ビュー (過去 30 日間)
Rania Fahmy
Rania Fahmy 2018 年 12 月 14 日
コメント済み: Rania Fahmy 2018 年 12 月 14 日
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 件のコメント
Rania Fahmy
Rania Fahmy 2018 年 12 月 14 日
編集済み: Rania Fahmy 2018 年 12 月 14 日
they do have to be consecutive yes
Fangjun Jiang
Fangjun Jiang 2018 年 12 月 14 日
I can suggest utilizing sparse matrix, see sparse().

サインインしてコメントする。

採用された回答

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 Center および File ExchangeParallel Computing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by