필터 지우기
필터 지우기

I need to split sequance of binary numbers to groups with step log2m

조회 수: 4 (최근 30일)
I need to split sequance of binary numbers to groups with step log2m
for example i need to let user input sequance of bits in array then this squance divide into groups with step log2m and each group tansfer to decimal and put them in array again

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 5월 14일
Run this example
x = '100100111100001111';
m = 8;
n = floor(log2(m));
x = [repmat('0', 1, ceil(numel(x)/n)*n-numel(x)) x]; % pad values so that digits
% can be evenly divided in
% groups of m
x = reshape(x, n, []).';
y = bin2dec(x)
  댓글 수: 11
Mostafa Salah
Mostafa Salah 2020년 5월 16일
great i need the last thing to connect this code with the another one what i mean the user input the number of zeros and ones and then divide them accoeding to log2M please thank you <3
x = [1 0 0 1 0 1 1 1 1 0 0 0 0 1 1 1 1];
m = 8;
n = floor(log2(m));
% pad zeros so that length become multiple of n
if ~(round(numel(x)/n)==numel(x)/n)
x = [zeros(1, ceil(numel(x)/n)*n-numel(x)) x]; %
end
x = reshape(x, n, []).';
y = x*(2.^(n-1:-1:0)).';% multiply with [2^(n-1) 2^(n-1) ... 2^1 2^0] to convert binary to decimal
Ameer Hamza
Ameer Hamza 2020년 5월 16일
편집: Ameer Hamza 2020년 5월 16일
while true
num = input('please input number of values you need to enter: ');
if isnumeric(num) && round(num)==num % check if it is integer
break;
else
fprintf('Value must be an integer\n');
end
end
x = zeros(1, num);
for i=1:num
while true
xii = input('Input value [0 or 1]: ');
if isnumeric(xii) && any(xii==[0 1]) % check if it is integer
break;
else
fprintf('Value must be 0 or 1\n');
end
end
x(i)=xii;
end
m = 8;
n = floor(log2(m));
% pad zeros so that length become multiple of n
if ~(round(numel(x)/n)==numel(x)/n)
x = [zeros(1, ceil(numel(x)/n)*n-numel(x)) x]; %
end
x = reshape(x, n, []).';
y = x*(2.^(n-1:-1:0)).';% multiply with [2^(n-1) 2^(n-1) ... 2^1 2^0] to convert binary to decimal

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Red에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by