{"id":1039,"date":"2024-10-21T14:57:27","date_gmt":"2024-10-21T09:27:27","guid":{"rendered":"https:\/\/codexplained.in\/?p=1039"},"modified":"2025-11-24T15:31:27","modified_gmt":"2025-11-24T10:01:27","slug":"transpose-of-a-matrix","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=1039","title":{"rendered":"Transpose of a Matrix"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p>Transposing a matrix is an important operation in mathematics and computer science. It involves converting the rows of a matrix into columns and the columns into rows. For example, if we have a matrix AAA defined as:A=[123456]A = \\begin{bmatrix} 1 &amp; 2 &amp; 3 \\\\ 4 &amp; 5 &amp; 6 \\end{bmatrix}A=[14\u200b25\u200b36\u200b]<\/p>\n\n\n\n<p>The transpose of matrix AAA, denoted as ATA^TAT, would be:AT=[142536]A^T = \\begin{bmatrix} 1 &amp; 4 \\\\ 2 &amp; 5 \\\\ 3 &amp; 6 \\end{bmatrix}AT=\u200b123\u200b456\u200b\u200b<\/p>\n\n\n\n<p>This operation is commonly used in various fields, including linear algebra, computer graphics, and data processing.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\n#define MAX 10  \/\/ Maximum size of the matrix\n\nvoid inputMatrix(int matrix&#x5B;MAX]&#x5B;MAX], int rows, int cols) {\n    printf(&quot;Enter elements of the matrix (%d x %d):\\n&quot;, rows, cols);\n    for (int i = 0; i &lt; rows; i++) {\n        for (int j = 0; j &lt; cols; j++) {\n            scanf(&quot;%d&quot;, &amp;matrix&#x5B;i]&#x5B;j]);\n        }\n    }\n}\n\nvoid transposeMatrix(int matrix&#x5B;MAX]&#x5B;MAX], int transpose&#x5B;MAX]&#x5B;MAX], int rows, int cols) {\n    for (int i = 0; i &lt; rows; i++) {\n        for (int j = 0; j &lt; cols; j++) {\n            transpose&#x5B;j]&#x5B;i] = matrix&#x5B;i]&#x5B;j];  \/\/ Swap rows and columns\n        }\n    }\n}\n\nvoid printMatrix(int matrix&#x5B;MAX]&#x5B;MAX], int rows, int cols) {\n    printf(&quot;Matrix (%d x %d):\\n&quot;, rows, cols);\n    for (int i = 0; i &lt; rows; i++) {\n        for (int j = 0; j &lt; cols; j++) {\n            printf(&quot;%d &quot;, matrix&#x5B;i]&#x5B;j]);\n        }\n        printf(&quot;\\n&quot;);\n    }\n}\n\nint main() {\n    int matrix&#x5B;MAX]&#x5B;MAX], transpose&#x5B;MAX]&#x5B;MAX];\n    int rows, cols;\n\n    printf(&quot;Enter number of rows and columns: &quot;);\n    scanf(&quot;%d %d&quot;, &amp;rows, &amp;cols);\n\n    inputMatrix(matrix, rows, cols);\n    transposeMatrix(matrix, transpose, rows, cols);\n    \n    printf(&quot;Original Matrix:\\n&quot;);\n    printMatrix(matrix, rows, cols);\n    \n    printf(&quot;Transposed Matrix:\\n&quot;);\n    printMatrix(transpose, cols, rows);\n\n    return 0;\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Includes and Defines<\/strong>: The program begins by including the <code>stdio.h<\/code> header for input\/output functions and defining a constant <code>MAX<\/code> for the maximum size of the matrix.<\/li>\n\n\n\n<li><strong>Input Function<\/strong>: <code>inputMatrix<\/code> prompts the user to enter elements for the matrix. It uses nested loops to read values into a two-dimensional array.<\/li>\n\n\n\n<li><strong>Transpose Function<\/strong>: <code>transposeMatrix<\/code> swaps elements of the original matrix to create the transpose. For each element at position (i, j) in the original matrix, it assigns its value to (j, i) in the transposed matrix.<\/li>\n\n\n\n<li><strong>Print Function<\/strong>: <code>printMatrix<\/code> displays the matrix in a readable format, iterating through each element.<\/li>\n\n\n\n<li><strong>Main Function<\/strong>: In <code>main<\/code>, the program:\n<ul class=\"wp-block-list\">\n<li>Declares two matrices: the original and its transpose.<\/li>\n\n\n\n<li>Asks for the number of rows and columns.<\/li>\n\n\n\n<li>Calls the input, transpose, and print functions to display the results.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Input<\/h3>\n\n\n\n<p>The user is prompted to enter the number of rows and columns, followed by the matrix elements. Example input:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nEnter number of rows and columns: 3 2\nEnter elements of the matrix (3 x 2):\n1 2\n3 4\n5 6\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<p>The program will output both the original and transposed matrices. Continuing from the previous example, the output will be:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nOriginal Matrix:\n1 2 \n3 4 \n5 6 \n\nTransposed Matrix:\n1 3 5 \n2 4 6 \n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>This C program effectively demonstrates how to compute the transpose of a matrix using basic array manipulation. Understanding how to transpose matrices is crucial for various applications in mathematics and computer science. The code can be easily modified to handle larger matrices or different data types, making it a versatile example for learning matrix operations in C.<\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Introduction Transposing a matrix is an important operation in mathematics and computer science. It involves converting the rows of a matrix into columns and the columns into rows. For example, if we have a matrix AAA defined as:A=[123456]A = \\begin{bmatrix} 1 &amp; 2 &amp; 3 \\\\ 4 &amp; 5 &amp; 6 \\end{bmatrix}A=[14\u200b25\u200b36\u200b] The transpose of [&hellip;]<\/p>\n","protected":false},"author":47,"featured_media":1116,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-1039","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1039","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/47"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1039"}],"version-history":[{"count":4,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1039\/revisions"}],"predecessor-version":[{"id":1383,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1039\/revisions\/1383"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/1116"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1039"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}