Program Conversion Help

This is a discussion on Program Conversion Help within the C Programming forums, part of the General Programming Boards category; Hi. I am a college student who is doing research over the summer and one of my tasks is to ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    45

    Program Conversion Help

    Hi. I am a college student who is doing research over the summer and one of my tasks is to convert a Wave Propagation Simulation code from MatLab into C. Needless to say, I have only very basic experience with C and I need a lot of help.

    First off, I want to know if it is possible to write C code that will take the Kronecker Product of two matrices. I've only come across code that performs normal matrix multiplication thus far. If I can get assistance on that, it would be great.

    Also, I'm having trouble with multidimensional arrays, which I obviously need.
    I have 6 different cases (A-F), each that creates a different matrix of a different length/size than the others. I want to know how I am supposed to initialize my array before my switch statement so that I can store the values I want in it for each case. For example, in case A, the matrix needs to be a 2x1 matrix, but if case B were chosen, the matrix would need to be a 2x9 matrix.

    I was also wondering about passing arrays to functions. I read a little bit about pointers, but I am not completely understanding how they work.

    I know this is a lot of information/questions, but I hope someone will be able to assist me.

    Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you can do it, you can program it. The Kronecker product doesn't look like it would be that bad.

    The main problem is that matrices are not as fundamental to C as they are to Matlab. More to the point for you, a 2x1 matrix and a 2x9 matrix are not really compatible at all. You can perhaps build a struct that contains the dimensions and has a pointer to an array. In any rate, you're going to become an expert on memory management.

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by magda3227 View Post
    [whether] it is possible to write C code that [...]
    Well, anything is possible really, you just have to have the time to implement it. To get the most out of this forum, ask specific questions. Start out with the most basic thing you want to do and show us what you have so far, we'll then be able to advise you on what to do.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    45

    My code thus far

    I have been able to make code that takes the Kronecker product of two matrices and store it within a new matrix. However, if the first matrix being multiplied has more rows or columns than the second matrix, the program gets stuck or returns incorrect values. I wanted to know if anyone can help me figure out how to fix this. Also, my coding is a lot of nested loops. I'm not very good at programming and feel that there is a less confusing/shorter code to accomplish the same thing. If there are any suggestions, please let me know.

    Code:
    #include <stdio.h>
    
    
    /*Computes the Kronecker Product of two 2x2 matrices. If matrix A has more rows or columns than matrix B, the program does not finish. However, if matrix B has more rows or columns than A, the code works properly.*/
    
    int main() 
    {
    
    	int cola, rowa, colb, rowb;
    	cola = 2;
    	rowa = 2;
    	colb = 2;
    	rowb = 2;
    
       int A[2][2] = {{1, 2}, {3, 4}},
           B[2][2] = {{0, 5},{6,7}};
    	int C[rowa*rowb][cola*colb];
    
    	
    	
    int i=0, j=0, k=0,l=0;
    
    while(i<rowa-1)
    {  
    do{
    	while(l<rowb-1)
    {
    
    	
      do{
    	C[i+l][j+k] = A[i][j]*B[l][k];
    	printf("%d\t",C[i+l][j+k]);
    	k++;
    	}while (k<colb);
    	k=0; j++; 
    
       do{
    	C[i+l][j+k+1] = A[i][j]*B[l][k];
    	printf("%d\t",C[i+l][j+k+1]);
    	k++;
    	}while (k<colb);
    	k=0; j=0; l++;
    
    	printf("\n");
    
    
    
     do{
    	C[i+l][j+k] = A[i][j]*B[l][k];
    	printf("%d\t",C[i+l][j+k]);
    	k++;
    	}while (k<colb);
    	 k=0; j++;
    
       do{
    	C[i+l][j+k+1] = A[i][j]*B[l][k];
    	printf("%d\t",C[i+l][j+k+1]);
    	k++;
    	}while (k<colb);
    	k=0;  j=0;  i++; l=0; 
    
    	printf("\n");
    
    do{
    	
    	C[i+l+1][j+k] = A[i][j]*B[l][k];
    	printf("%d\t",C[i+l+1][j+k]);
    	k++;
    	}while (k<colb);
    	k=0; j++; 
    
    	
       do{
    	C[i+l+1][j+k+1] = A[i][j]*B[l][k];
    	printf("%d\t",C[i+l+1][j+k+1]);
    	k++;
    	}while (k<colb);
    	 k=0;  j=0; l++; 
    
    	printf("\n");  
    
     do{
    	C[i+l+1][j+k] = A[i][j]*B[l][k];
    	printf("%d\t",C[i+l+1][j+k]);
    	k++;
    	}while (k<colb);
    	k=0; j++; 
    
    	
       do{
    
    	C[i+l+1][j+k+1] = A[i][j]*B[l][k];
    	printf("%d\t",C[i+l+1][j+k+1]);
    	k++;
    	}while (k<colb);
    	k=0;
    
    printf("\n");
    
    }l=0; }while(j<cola-1);
    } 
    
    
    	/*printf("\n\n%d",C[0][3]);*/
    
    
    
    return 0;
    }

    Thanks.

  5. #5
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    When trying to come up with an algorithm for such a scenario, dividing it up into possible cases would help. Creating one algorithm that can handle dynamic sizes of (insert something here) is often hard, especially in this case by the looks of wikipedia.

    With matrix A and B, the cases are:

    1. A and B have same number of rows and columns.

    2. Either one has more columns.

    3. Either one has more rows.

    4. Either one has more columns and rows.

    5. Either one has more rows and the other has more columns.

    Alright, five cases. Some might even be possible to migrate into the other cases/functions.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should be able to figure out a priori the size of C, which you have. The indices move in odometer style -- the second one moves faster, and when it resets the first one increments. And in each case, the first index goes around only once.

    Hint: Consider a first matrix with dimension 3 and second matrix with dimension 5 (rows or columns, doesn't matter right now). That means the result should have dimension 15. Compute a/5 (integer division, so throw away remainder) and a%5 (here's the remainder) as a goes from 0 to 14. It starts 0,0; then 0,1; then 0,2; then 0,3; then 0,4; then 1,0; then 1,1; ...; then 2,4. This may give you some idea as to how to figure out indices.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    45

    Got it

    I overcomplicated it. The code that works for any two matrices is the following:

    Code:
    #include <stdio.h>
    
    
    /*Computes the Kronecker Product of two matrices and stores it as matrix C*/
    
    int main() 
    {
    
    	int cola, rowa, colb, rowb;
    	cola = 2;
    	rowa = 3;
    	colb = 3;
    	rowb = 2;
          int a = rowa/rowb, b=rowa%rowb, c=cola/colb, d=cola%colb, e=1;
    
    
       int A[3][2] = {{1, 2}, {3, 4}, {1,0}},
           B[2][3] = {{0, 5, 2},{6,7, 3}};
    	int C[rowa*rowb][cola*colb];
    
    	int i,j,k,l;
    	
    
    	for(i=0; i<rowa; i++)
    	{
    		for(k=0; k<rowb; k++)
    			{
    				for(j=0; j<cola; j++)
    					{
    						for(l=0; l<colb; l++)
    							{
    								
    								C[i+l+1][j+k+1] = A[i][j]*B[k][l];
    								printf("%d\t",C[i+l+1][j+k+1]);
    
    							}
    					}printf("\n");
    			} 
    	}
    	
    	
    	return 0;
    }
    Thanks to everyone who helped.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. Decimal to Binary Conversion program
    By acidbeat311 in forum C Programming
    Replies: 5
    Last Post: 01-12-2006, 10:26 PM
  4. question about the loop in case conversion program
    By Elhaz in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2004, 05:06 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21