%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% Matrix A is the top left 16x512 submatrix of OEIS A195467 (consecutive Gray code permutations)
%%%%%%%%% and shows the 16 different powers of the 9-bit gray code permutation:

g8=[1 0 0 0 0 0 0 0;
    0 1 0 0 0 0 0 0;
    0 0 0 1 0 0 0 0;
    0 0 1 0 0 0 0 0;
    0 0 0 0 0 0 0 1;
    0 0 0 0 0 0 1 0;
    0 0 0 0 1 0 0 0;
    0 0 0 0 0 1 0 0];

g16=[g8 zeros(8);
zeros(8) rot90(g8)'];

g32=[g16 zeros(16);
zeros(16) rot90(g16)'];

g64=[g32 zeros(32);
zeros(32) rot90(g32)'];

g128=[g64 zeros(64);
zeros(64) rot90(g64)'];

g256=[g128 zeros(128);
zeros(128) rot90(g128)'];

g512=[g256 zeros(256);
zeros(256) rot90(g256)'];

A = [ [0:511];               
      [0:511]*g512;
      [0:511]*g512^2;
      [0:511]*g512^3;
      [0:511]*g512^4;
      [0:511]*g512^5;
      [0:511]*g512^6;
      [0:511]*g512^7;
      [0:511]*g512^8 
      [0:511]*g512^9;
      [0:511]*g512^10;
      [0:511]*g512^11;
      [0:511]*g512^12;
      [0:511]*g512^13;
      [0:511]*g512^14;
      [0:511]*g512^15 ];

                 %%% whos A
                 %%% Name       Size             Bytes  Class     Attributes

                 %%% A         16x512            65536  double              

                 %%% A(:,1:32) =
                 %%% 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
                 %%% 0  1  3  2  6  7  5  4 12 13 15 14 10 11  9  8 24 25 27 26 30 31 29 28 20 21 23 22 18 19 17 16
                 %%% 0  1  2  3  5  4  7  6 10 11  8  9 15 14 13 12 20 21 22 23 17 16 19 18 30 31 28 29 27 26 25 24
                 %%% 0  1  3  2  7  6  4  5 15 14 12 13  8  9 11 10 30 31 29 28 25 24 26 27 17 16 18 19 22 23 21 20
                 %%% 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 17 16 19 18 21 20 23 22 25 24 27 26 29 28 31 30
                 %%% 0  1  3  2  6  7  5  4 12 13 15 14 10 11  9  8 25 24 26 27 31 30 28 29 21 20 22 23 19 18 16 17
                 %%% 0  1  2  3  5  4  7  6 10 11  8  9 15 14 13 12 21 20 23 22 16 17 18 19 31 30 29 28 26 27 24 25
                 %%% 0  1  3  2  7  6  4  5 15 14 12 13  8  9 11 10 31 30 28 29 24 25 27 26 16 17 19 18 23 22 20 21
                 %%% 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
                 %%% 0  1  3  2  6  7  5  4 12 13 15 14 10 11  9  8 24 25 27 26 30 31 29 28 20 21 23 22 18 19 17 16
                 %%% 0  1  2  3  5  4  7  6 10 11  8  9 15 14 13 12 20 21 22 23 17 16 19 18 30 31 28 29 27 26 25 24
                 %%% 0  1  3  2  7  6  4  5 15 14 12 13  8  9 11 10 30 31 29 28 25 24 26 27 17 16 18 19 22 23 21 20
                 %%% 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 17 16 19 18 21 20 23 22 25 24 27 26 29 28 31 30
                 %%% 0  1  3  2  6  7  5  4 12 13 15 14 10 11  9  8 25 24 26 27 31 30 28 29 21 20 22 23 19 18 16 17
                 %%% 0  1  2  3  5  4  7  6 10 11  8  9 15 14 13 12 21 20 23 22 16 17 18 19 31 30 29 28 26 27 24 25
                 %%% 0  1  3  2  7  6  4  5 15 14 12 13  8  9 11 10 31 30 28 29 24 25 27 26 16 17 19 18 23 22 20 21

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% Matrix B0 is the top left 16x512 submatrix of OEIS A197819 
%%%%%%%%% and shows 16 binary Walsh functions w(A001317) of length 512:

%%% H is the binary Walsh matrix of order 512:

H = (hadamard(512)-1)/(-2) ;

                 %%% whos H
                 %%% Name        Size               Bytes  Class     Attributes

                 %%% H         512x512            2097152  double              

                 %%% H(1:16,1:16) =
                 %%% 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
                 %%% 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
                 %%% 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
                 %%% 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0
                 %%% 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
                 %%% 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0
                 %%% 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0
                 %%% 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1
                 %%% 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
                 %%% 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0
                 %%% 0 0 1 1 0 0 1 1 1 1 0 0 1 1 0 0
                 %%% 0 1 1 0 0 1 1 0 1 0 0 1 1 0 0 1
                 %%% 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0
                 %%% 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1
                 %%% 0 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1
                 %%% 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0

%%% Define binary Walsh functions w(A001317) of lengh 512:
  % The first 16 elements of OEIS A001317 (Sierpinski triangle rows read like binary numbers) are:
  % 1, 3, 5, 15,    17, 51, 85, 255,    257, 771, 1285, 3855,    4369, 13107, 21845, 65535
  % As these numbers exceed the size of H
  % they are replaced by their modulo 512 values:
  % 1, 3, 5, 15,    17, 51, 85, 255,    257, 259,  261,  271,     273,   307,   341,   511
  % As Walsh functions are counted from 0 and MATLAB counts indexes from 1
  % the 1 has to be added to each of these numbers.
  
B0 = cell2mat( { H(   1 + 1 , : ) ; 
                 H(   3 + 1 , : ) ; 
                 H(   5 + 1 , : ) ; 
                 H(  15 + 1 , : ) ;   
                
                 H(  17 + 1 , : ) ; 
                 H(  51 + 1 , : ) ; 
                 H(  85 + 1 , : ) ; 
                 H( 255 + 1 , : ) ;      
                
                 H( 257 + 1 , : ) ; 
                 H( 259 + 1 , : ) ; 
                 H( 261 + 1 , : ) ; 
                 H( 271 + 1 , : ) ;   
                
                 H( 273 + 1 , : ) ; 
                 H( 307 + 1 , : ) ; 
                 H( 341 + 1 , : ) ; 
                 H( 511 + 1 , : ) } ) ;

%%% B0 is repeatedly streched:
  % function [y] = strech(x)
  %     y = zeros(16,512) ;
  %     for k=1:256
  %     y(1:16,2*k-1) = x(1:16,k) ;
  %     y(1:16,2*k) = x(1:16,k) ;
  %     end
  % end

B1 = strech(B0) ;
B2 = strech(B1) ;
B3 = strech(B2) ;
B4 = strech(B3) ;
B5 = strech(B4) ;
B6 = strech(B5) ;
B7 = strech(B6) ;
B8 = strech(B7) ;

                 %%% whos B0 B1 B2 
                 %%% Name       Size             Bytes  Class     Attributes
              
                 %%% B0        16x512            65536  double              
                 %%% B1        16x512            65536  double              
                 %%% B2        16x512            65536  double              

                 %%% etc.          

                 %%% B0(:,1:32) =
                 %%% 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
                 %%% 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0
                 %%% 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0
                 %%% 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0
                 %%% 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
                 %%% 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1
                 %%% 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1
                 %%% 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1
                 %%% 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
                 %%% 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0
                 %%% 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0
                 %%% 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0
                 %%% 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
                 %%% 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1
                 %%% 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1
                 %%% 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1

                 %%% B1(:,1:32) =
                 %%% 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
                 %%% 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0
                 %%% 0 0 1 1 0 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1 0 0 1 1 1 1 0 0 1 1 0 0
                 %%% 0 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 0 0 1 1 1 1 0 0
                 %%% 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
                 %%% 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0
                 %%% 0 0 1 1 0 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1 0 0 1 1 1 1 0 0 1 1 0 0
                 %%% 0 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 0 0 1 1 1 1 0 0
                 %%% 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
                 %%% 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0
                 %%% 0 0 1 1 0 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1 0 0 1 1 1 1 0 0 1 1 0 0
                 %%% 0 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 0 0 1 1 1 1 0 0
                 %%% 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
                 %%% 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0
                 %%% 0 0 1 1 0 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1 0 0 1 1 1 1 0 0 1 1 0 0
                 %%% 0 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 0 0 1 1 1 1 0 0

                 %%% B2(:,1:32) =
                 %%% 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
                 %%% 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0
                 %%% 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0
                 %%% 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1
                 %%% 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
                 %%% 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0
                 %%% 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0
                 %%% 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1
                 %%% 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
                 %%% 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0
                 %%% 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0
                 %%% 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1
                 %%% 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
                 %%% 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0
                 %%% 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0
                 %%% 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1

                 %%% etc.

%%% The entries of B0,B1,... are the binary digits of the entries of Bsum:

Bsum   =   B0  +  2.*B1  +  4.*B2  +  8.*B3  +  16.*B4  +  32.*B5  +  64.*B6  +  128.*B7  +  256.*B8 ;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% The matrices A and Bsum are equal:

isequal(A,Bsum)
ans =
     1