# Binary To Gray Code Conversion in MATLAB without using MATLAB's Inbuilt Function

How a Binary code (of any length) is converted to a Gray Code, its a simple process requiring XORing  of 2 bits & saving the resultant bit in a new place. I have tried to explain this process by the following 2 diagrams. In this I have taken & assumed 4 bit arbitrary binary code, and operating it with XOR gates to convert to Gray code.
B(1), B(2), B(3) & B(4) collectively denotes the 4 bit binary, of which B(1) is the MSB. G(1), G(2), G(3) & G(4) collectively denotes the 4 bit gray code. GATE Level Logic Diagram of Binary to Gray Code Conversion MATLAB: How the Implementation of Binary Code To Gray Code conversion is done

The MATLAB code below for conversion of Binary Code to Gray code, will work for any binary vector or matrix, i.e., you not need to worry about the conversion of 'n x m' binary matrix to a 'n x 1' binary vector first, you simply feed in & the code will give the resultant Gray Code Matrix for the same Binary Code Matrix. The code below will then process row wise in order to calculate the Gray Code.

Suppose we are having a binary 10 x 10 matrix, so the code below will take 1 binary value row at 1 time then process it to give a equal sized gray coded row. The similar thing will be repeated with each row of that matrix. Even if its a single vector of 'n x 1' size this code will work.

MATLAB Code for conversion of Binary Code to Gray Code:
a=rand(10,10)>0.5;
%the variable 'a' contains the binary values in terms of a matrix currently its a 100x100 binary matrix %you can provide your own matrix of any size but remember it will calculate the gray code row wise
g(:,1) = a(:,1); %assign the 1st element of each row to the resultant 'g', as it is.
for i = 2:size(a,2)
g(:,i) = xor( a(:,i-1), a(:,i) );%processing as per the procedure
end

Example Execution:
With a 10x10 binary matrix,
a = %the binary matrix
0     1     1     0     0     1     1     1     1     1
0     0     0     0     1     0     0     1     1     0
0     0     0     1     1     0     0     1     1     1
1     0     0     1     0     1     1     1     1     1
1     1     1     0     0     0     0     0     1     1
1     0     0     0     0     1     0     1     0     1
0     1     1     0     0     1     0     0     0     1
1     0     1     0     0     1     0     1     0     1
1     0     1     1     1     1     0     1     1     0
1     0     1     0     1     0     0     0     0     1

g = %the gray coded matrix coded row wise
0     1     0     1     0     1     0     0     0     0
0     0     0     0     1     1     0     1     0     1
0     0     0     1     0     1     0     1     0     0
1     1     0     1     1     1     0     0     0     0
1     0     0     1     0     0     0     0     1     0
1     1     0     0     0     1     1     1     1     1
0     1     0     1     0     1     1     0     0     1
1     1     1     1     0     1     1     1     1     1
1     1     1     0     0     0     1     1     0     1
1     1     1     1     1     1     0     0     0     1

With a 1x30 binary vector,
a = %the binary vector
0     1     0     0     1     1     1     0     1     1     1     0     1     1     0     0     1     1     0     1     1     0     0     0     0     0     0     1     0     1

g = %the gray coded vector
0     1     1     0     1     0     0     1     1     0     0     1     1     0     1     0     1     0     1     1     0     1     0     0     0     0     0     1     1     1

Also Visit:
Gray To Binary Code Conversion in MATLAB without using MATLAB's Inbuilt Function