Effacer les filtres
Effacer les filtres

i have a single row vector contains 0s and 1s and want to convert every 3 bits into integer how can i do that??

1 vue (au cours des 30 derniers jours)
i have a single row vector contains 0s and 1s and want to convert every 3 bits in this vector into integer how can i do that??
i have single row vector contains 0s and 1s values and i need every 3 bits in this vector convert to decimal number
for example
x=[0 1 0 1 1 1 ]
then i want this single row to be converted to decimel every 3 of them to decimal [010],[111]

Réponse acceptée

Mathieu NOE
Mathieu NOE le 22 Avr 2022
hello
see below
hope it helps
% dummy data
nbits = 3;
samples = 8;
x = round(rand(1,nbits*samples));
nbits_vect = 2.^((nbits-1:-1:0));
%%%% main loop %%%%
iter = fix((length(x)-nbits)/nbits +1);
for ci=1:iter
start_index = 1+(ci-1)*nbits;
stop_index = min(start_index+ nbits-1,length(x));
int_data(ci) = sum(nbits_vect.*x(start_index:stop_index)); % your interger data
end
figure(1),
plot(int_data,'r'); % your interger data
  6 commentaires
Aly Khafagy
Aly Khafagy le 24 Avr 2022
hello Mathew, i need to know what is meant by nbits_vect ?? i cannot understand this variable.
Bruno Luong
Bruno Luong le 24 Avr 2022
Modifié(e) : Bruno Luong le 24 Avr 2022
it's equivalent to the sequence
[100,10,1] but in base 2.

Connectez-vous pour commenter.

Plus de réponses (2)

Bruno Luong
Bruno Luong le 22 Avr 2022
x=[0 1 0 1 1 1 ];
bin2dec(char(reshape(x,3,[])+'0')')
ans = 2×1
2 7

Voss
Voss le 22 Avr 2022
Modifié(e) : Voss le 22 Avr 2022
You say you want [010,111] as a result:
x=[0 1 0 1 1 1 ];
xx = reshape(x,3,[]).';
y = zeros(1,size(xx,1));
for ii = 1:size(xx,1)
y(ii) = polyval(xx(ii,:),10);
end
disp(y)
10 111
If you want [2,7] as a result:
for ii = 1:size(xx,1)
y(ii) = polyval(xx(ii,:),2);
end
disp(y)
2 7

Catégories

En savoir plus sur MATLAB 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