Effacer les filtres
Effacer les filtres

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

4 vues (au cours des 30 derniers jours)
Mostafa Salah
Mostafa Salah le 14 Mai 2020
Modifié(e) : Ameer Hamza le 16 Mai 2020
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

Réponse acceptée

Ameer Hamza
Ameer Hamza le 14 Mai 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 commentaires
Mostafa Salah
Mostafa Salah le 16 Mai 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 le 16 Mai 2020
Modifié(e) : Ameer Hamza le 16 Mai 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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Red dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by