another ! transposition gives wrong results!

Could someone please explain me why I am not taking the right results??
I can't understand what I have wrong!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>


int main( int argc, const char* argv[] ){

	int m = 3 , n = 2 , k = 4;  
	
	float A[ m * n ] , B[ n * k ];
	float AColM[ n * m ] , BColM[ k * n ];
	float Result[ m * k ];

	// assume row major order
	A[0] = 1.0;
	A[1] = 2.0;
	A[2] = 3.0;
	A[3] = 4.0;
	A[4] = 5.0;
	A[5] = 6.0;
	
	B[0] = 0.0;
	B[1] = 1.0;
	B[2] = 3.0;
	B[3] = 1.0;
	B[4] = 0.0;
	B[5] = 2.0;
	B[6] = 4.0;
	B[7] = 5.0;
	
	//print the above matrices ( row major )
	printf("\n Row major : \n");
	for ( int i = 0; i < m; i++ )
		for ( int j = 0; j < n; j++ ) {
			printf("\nA[%d][%d] = %f",i,j,A[ i * n + j ]);
			if( j == n-1 )
            	printf("\n");
            }
	printf("\n");
	
	for ( int i = 0; i < n; i++ )
		for ( int j = 0; j < k; j++ ) {
			printf("\nB[%d][%d] = %f",i,j,B[ i * k + j ]);
			if( j == k-1 )
            	printf("\n");
            }
			printf("\n\n");
	
	
	// make the above matrices in column major order
	for ( int i = 0; i < m; i++ )
		for ( int j = 0; j < n; j++ )
			AColM[ j * m + i ] = A[ i * n + j ];
	
	for ( int i = 0; i < n; i++ )
		for ( int j = 0; j < k; j++ )
			BColM[ j * n + i ] = B[ i * k + j ];
	
	
	//print the above matrices ( column major )
	printf("\n Column major : \n");
	for ( int i = 0; i < n; i++ )
		for ( int j = 0; j < m; j++ ) {
			printf("\nA[%d][%d] = %f",i,j,AColM[ i * n + j ]);
			if( j == m-1 )
            	printf("\n");
            }
	printf("\n");
	
	for ( int i = 0; i < k; i++ )
		for ( int j = 0; j < n; j++ ){
			printf("\nB[%d][%d] = %f",i,j,BColM[ i * k + j ]);
			if( j == n-1 )
            	printf("\n");
            }
	printf("\n\n");       

  return 0;
}
Results:

Row major :

A[0][0] = 1.000000
A[0][1] = 2.000000

A[1][0] = 3.000000
A[1][1] = 4.000000

A[2][0] = 5.000000
A[2][1] = 6.000000


B[0][0] = 0.000000
B[0][1] = 1.000000
B[0][2] = 3.000000
B[0][3] = 1.000000

B[1][0] = 0.000000
B[1][1] = 2.000000
B[1][2] = 4.000000
B[1][3] = 5.000000



Column major :

A[0][0] = 1.000000
A[0][1] = 3.000000
A[0][2] = 5.000000

A[1][0] = 5.000000
A[1][1] = 2.000000
A[1][2] = 4.000000


B[0][0] = 0.000000
B[0][1] = 0.000000

B[1][0] = 3.000000
B[1][1] = 4.000000

B[2][0] = 0.008488
B[2][1] = 0.000000

B[3][0] = 1.000000
B[3][1] = 3.000000

It gives me random results every time compiling.Some of the results are ok

Comments

  • In this line
    printf("\nA[%d][%d] = %f", i, j, AColM[i * n + j]);
    
    You should be using m as the width rather than n.
    printf("\nA[%d][%d] = %f", i, j, AColM[i * m + j]);
    

    Same thing for the BColM matrix.
  • You are right!
    Thanks for answering!

    I must tell you though ,that I am confused now because I found this:
        #include <iostream>
    
        void printrowmajor(int* arr, int width, int height)
        {
        for (int i = 0; i < height; ++i)
        {
        for (int j = 0; j < width; ++j)
        std::cout<<arr[i * width + j]<<" ";
        std::cout<<"\n";
        }
    
        }
    
        void printcolumnmajor(int* arr, int width, int height)
        {
        for (int i = 0; i < width; ++i)
        {
        for (int j = 0; j < height; ++j)
        std::cout<<arr[i + j * width]<<" ";
        std::cout<<"\n";
        }
        }
    
    
        int main()
        {
        int arr[12] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
        printrowmajor(&arr[0], 4, 3);
        std::cout<<"\n";
        printcolumnmajor(&arr[0], 4, 3);
        return 0;
        }
    

    which works by printing the transposed matrix using column major order .

    We are printing matrix using row major order ( i * m + j ).

    And both have their loops "reversed".

    How this can happen!
Sign In or Register to comment.