Borrar filtros
Borrar filtros

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

4 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 14 de Mayo de 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 comentarios
Mostafa Salah
Mostafa Salah el 16 de Mayo de 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 el 16 de Mayo de 2020
Editada: Ameer Hamza el 16 de Mayo de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Red en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by