The transpose of a matrix is the matrix obtained by replacing all elements with
The following C code let’s the user enter a matrix, and returns it’s transpose, .
CODE:
/************************************************** *************MATRIX TRANSPOSE***************** **************************************************/ #include<stdio.h> /******* Function that calculates the transpose of matrices: There are two options to do this in C. 1. Pass a matrix (trans) as the parameter, and calculate and store the transpose in it. 2. Use malloc and make the function of pointer type and return the pointer. This program uses the first option. ********/ void matTranspose(int m, int n, double a[m][n], double trans[n][m]){ int i,j,k; for(i=0;i<n;i++){ for(j=0;j<m;j++){ trans[i][j]=a[j][i]; } } } /******* Function that reads the elements of a matrix row-wise Parameters: rows(m),columns(n),matrix[m][n] *******/ void readMatrix(int m, int n, double matrix[m][n]){ int i,j; for(i=0;i<m;i++){ for(j=0;j<n;j++){ scanf("%lf",&matrix[i][j]); } } } /******* Function that prints the elements of a matrix row-wise Parameters: rows(m),columns(n),matrix[m][n] *******/ void printMatrix(int m, int n, double matrix[m][n]){ int i,j; for(i=0;i<m;i++){ for(j=0;j<n;j++){ printf("%lf\t",matrix[i][j]); } printf("\n"); } } int main(){ int m,n,i,j; printf("Enter the size of the matrix:\nNo. of rows (m): "); scanf("%d",&m); printf("\nNo. of columns (n): "); scanf("%d",&n); double a[m][n]; double trans[n][m]; printf("\nEnter the elements of matrix:\n"); readMatrix(m,n,a); matTranspose(m,n,a,trans); printf("\nThe transpose of the matrix is:\n"); printMatrix(n,m,trans); }
OUTPUT:
Android Apps:
I’ve also created a few Android apps that perform various matrix operations and can come in handy to those taking a course on Numerical Methods.
Download: https://play.google.com/store/apps/details?id=com.bragitoff.numericalmethods
Download: https://play.google.com/store/apps/details?id=com.bragitoff.matrixcalculator
References and Resources:
https://en.wikipedia.org/wiki/Transposehttp://mathworld.wolfram.com/Transpose.html
Ph.D. researcher at Friedrich-Schiller University Jena, Germany. I’m a physicist specializing in computational material science. I write efficient codes for simulating light-matter interactions at atomic scales. I like to develop Physics, DFT, and Machine Learning related apps and software from time to time. Can code in most of the popular languages. I like to share my knowledge in Physics and applications using this Blog and a YouTube channel.