필터 지우기
필터 지우기

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 CenterFile Exchange에서 Parallel Computing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by