How to Convert Matrix Rows to Binary Numbers in MATLAB?

  • Context: MATLAB 
  • Thread starter Thread starter xentity1x
  • Start date Start date
  • Tags Tags
    Data Matlab Type
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 19K views
xentity1x
Messages
9
Reaction score
0
I'm writing a program in matlab. I have a matrix whose entries are all ones and zeros. I want to convert each row vector of the matrix into a binary number whose digits are the entries of the vector. So for example if an arbitrary row of the matrix was [1, 0, 1, 1], I would like to convert it into the binary number 1011. How would I go about doing that? My end goal is to convert that binary number into a base ten one, but I already know how to do that.
 
Physics news on Phys.org
Well, it could be done by using the following simple formula:
[tex]nbr=\sum_{i=1}^{N}a_{i}10^{N-i}[/tex]

where a_i are the elements of the vector and N is the dimension.
 
In case you have numeric (double integer) valued matrix components, then you can use the following MATLAB conversion:

B=dec2bin(A);

then back to double integer you can convert using:
C=bin2dec(B); % NB: You will loose however decimal pointsHopefully that helps you.