### Highlights from dec2gc.m

3.75
3.8 | 4 ratings Rate this file 2 Downloads (last 30 days) File Size: 1.54 KB File ID: #8051 Version: 1.0

# dec2gc.m

### Arjun Srinivasan Rangamani (view profile)

18 Jul 2005 (Updated )

This function converts a decimal number to its equivalent gray code representation

File Information
Description

The function dec2gc converts a real positive numeric column vector to its gray code equivalent with a specified level of precision.

Example:
real_num = [34;78;123;7];%set of real numbers to be converted
N = 7;%precision level for gray coding
gray_code = dec2gc(real_num,N);

Result:
gray_code =

0 1 1 0 0 1 1
1 1 0 1 0 0 1
1 0 0 0 1 1 0
0 0 0 0 1 0 0

NOTE: N should be carefully chosen such that 2^N > max(dec)

Acknowledgements

This file inspired Walsh Functions and Genetic Algorithm To Optimise Schaffer's F6 Function.

MATLAB release MATLAB 6.5.1 (R13SP1)
26 Dec 2009 Dmitri M

### Dmitri M (view profile)

Darshong Chen.

Nice solution.

I used it for building sequency of Hadamard matrix of order N.

Something like:

function SeqOrd = GetSequency( N )

% N - [in] the order of Hadamard matrix
% SeqOrd - [out] the sequency, so that SeqOrd[i] is the sequency of row i
%
% Algorithm:
% 1. build a list of indexes 0..N-1
% 2. convert order of bits of each index
% 3. get the gray code value for each index ( that was converted)
% This gives the needed permutation with indexes range [0 .. N-1]

v = bitrevorder((0:N-1)'); % build list and reverse bit-order

SeqOrd = bitxor(v,bitshift(v,-1)); % find the gray-code

end

Comment only

Good Solution

11 Jul 2007 Cathy Williams

can we convert a matrix? yeh whatever, served the purpose for me!

Darshong Chen,

Excellent solution. Thanks!

24 Sep 2005 darnshong Chen

why not do like this:
numout=bitxor(numin,bitshift(numin,-1));

not need to loop.