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

11 views (last 30 days)
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

Accepted Answer

Ameer Hamza
Ameer Hamza on 14 May 2020
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 Comments
Mostafa Salah
Mostafa Salah on 16 May 2020
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 on 16 May 2020
Edited: Ameer Hamza on 16 May 2020
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

Sign in to comment.

More Answers (0)

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!