Effacer les filtres
Effacer les filtres

How to change element in base 10 to base 2 in a matrix, with loops and while

3 vues (au cours des 30 derniers jours)
yuval ohayon
yuval ohayon le 9 Sep 2017
Commenté : Walter Roberson le 24 Sep 2017
Suppose i have a matrix called A,the elements ib base 10,how do i convert to base 2 .with conditions and loops.
  14 commentaires
yuval ohayon
yuval ohayon le 24 Sep 2017
Modifié(e) : Walter Roberson le 24 Sep 2017
o.k i understood that i need define a matrix (veriable in the example) and the places of each element is indexed by the 'count' but how i store the last bit and adding the next one until i get a string like '1001001',say binnum=1001001 veriavle(count)=binnum; and
function [y] = dectobin(current_num)
binary_num=0
while (current_num~=0)
last_bit=mod(current_num,2); %is the function is right?what i have missing?
current_num=(current_num-last_bit)/2;
binary_num=lastbit;
Walter Roberson
Walter Roberson le 24 Sep 2017
variable(count) = lastbit;
Yes, mod(current_num,2) is fine to extract the last bit.

Connectez-vous pour commenter.

Réponses (1)

Jose Marques
Jose Marques le 9 Sep 2017
A = magic(5) %generating a matrix 5x5
A = (A<10) %suppose you want the elements in A < 10

Catégories

En savoir plus sur Data Type Conversion 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