フィルターのクリア

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

4 ビュー (過去 30 日間)
Mostafa Salah
Mostafa Salah 2020 年 5 月 14 日
編集済み: Ameer Hamza 2020 年 5 月 16 日
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 Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by