MATLAB How to Convert Matrix Rows to Binary Numbers in MATLAB?

  • Thread starter Thread starter xentity1x
  • Start date Start date
  • Tags Tags
    Data Matlab Type
AI Thread Summary
The discussion focuses on converting rows of a binary matrix in MATLAB into binary numbers and subsequently into base ten integers. The user seeks to transform a row vector, such as [1, 0, 1, 1], into the binary number 1011. A suggested method involves using MATLAB functions to facilitate this conversion. Specifically, the process includes using the `dec2bin` function to convert the matrix to binary format, followed by `bin2dec` to convert the binary string back to a decimal integer. It is noted that this method may result in the loss of decimal points if the original matrix contains numeric values. The formula provided for manual conversion emphasizes the summation of each binary digit multiplied by its positional value.
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:
nbr=\sum_{i=1}^{N}a_{i}10^{N-i}

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.
 

Similar threads

Replies
8
Views
2K
Replies
2
Views
2K
Replies
1
Views
2K
Replies
1
Views
1K
Replies
5
Views
3K
Replies
2
Views
2K
Back
Top