Thread: Hadamard matrix construction.

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    Hadamard matrix construction.

    Hi guys, I'm a beginner to C and I've been given a task to construct a 32 x 32 Hadamard matrix by copying a 2x2 into a 4x4 into a 8x8 and so on. I'm not sure why but my code doesn't work, yet when I used it in Matlab it worked. Of course, i changed it all to C syntax.

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <conio.h>
    
    
    main() {
    int H_SIZE=32;  /*length of each code word: you can change. */
    int n,r1,c1,i,j;
    int hadamard_matrix[2][2]={{1,1},{1,0}}; /* Initialise 1x1 matrix */
    
    
    for (r1=0;n<H_SIZE; r1*=2){
        for(c1=0; c1<H_SIZE;c1*=2){
            while (r1<H_SIZE){
                for (i=0;i<r1;i++){ /* loop#1: Copying the code matrix itself below for new code matrix */
                    for (j=0;j<c1; j++){
                    hadamard_matrix[i+r1][j]=hadamard_matrix[i][j];
                }
                }
                for (j=0; j<c1;j++){/* Loop#2: Copying the code matrix on right to itself for new matrix */
                    for (i=0; i<r1;i++){
                    hadamard_matrix[i][j+c1]=hadamard_matrix[i][j];
                    }
                }
                for (i=0;i<r1;i++){/* Loop#3: Copying cojugate of code matrix for complentary diagonal part */
                    for (j=0;j<c1;j++){
                    hadamard_matrix[i+r1][j+c1]=hadamard_matrix[i][j];}}
            }        
        }
    }
        printf("%d\t", hadamard_matrix);
    }
    Please help!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Why not do this the easy way?

    Make a separate function, the parameters for it will be the starting row and the starting column, and the ending row, and the ending column, for the copy you want at that moment.

    Call it as many times as you need to. Knocks off a lot of those loops, and it can also call itself, recursively.

    Your code is a good example of "clever as I am" type code, which is a stew of i,j,,r1,c1, etc. variables. As clever as it is, that's not a good way to program in C, especially as your programs get larger.

    When r1 and c1 are *= 2, and have a starting value of 0, I'm sure you're not getting what you wanted. I'm guessing that in Matlab, array indices start at 1 maybe?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well matlab may automatically expand arrays as you use them, but C does not.

    > int hadamard_matrix[2][2]
    This needs to be
    int hadamard_matrix[32][32]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    2
    Quote Originally Posted by Salem View Post
    Well matlab may automatically expand arrays as you use them, but C does not.

    > int hadamard_matrix[2][2]
    This needs to be
    int hadamard_matrix[32][32]
    Thanks for the reply, but if I just write the final matrix/array, how do I initialise it as a 2x2?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Can you initialize the array to be 32 X 32, but only USE and PRINT OUT the 2x2 part of it that you need, at first. Later, you might use the 4x4, the 8x8, etc.

    Code:
    int r,c;
    char matrix[32][32]={
    {"Near a lonely prison wall"},
    {"I heard a young girl calling"},
    {"Michael they have taken you away"},
    {"for you stole Travalyn's corn},
    {"so the young, might see the morn"},
    {"now a prison ship lies waiting in the bay"};
    {"Low lie the Fields of Athenry"},
    {"Where once we watched"},
    {"the small free birds fly"};
    ;
    
    for(r=0;r<2;r++) {
       for(c=0;c<2;c++) {
          printf("%c",matrix[r][c]);
       }
       printf("\n");
    }
    printf("\n\n");
    Try the above with 2, 4, and 8, and see if that helps

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > but if I just write the final matrix/array, how do I initialise it as a 2x2?
    Exactly like you are at the moment.
    What you have will fill the top-left 2x2 with those numbers, and the rest with zero.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default int construction
    By Mozza314 in forum C++ Programming
    Replies: 24
    Last Post: 03-23-2011, 11:53 AM
  2. construction order
    By George2 in forum C# Programming
    Replies: 4
    Last Post: 04-30-2008, 08:16 AM
  3. Why does C allow the following construction
    By cdalten in forum C Programming
    Replies: 21
    Last Post: 03-12-2006, 06:36 PM
  4. Firewall Construction
    By forsaken_dragon in forum Networking/Device Communication
    Replies: 22
    Last Post: 08-10-2005, 01:53 PM
  5. hadamard matrix
    By detfella in forum C++ Programming
    Replies: 3
    Last Post: 02-24-2004, 08:22 PM