{"id":1384,"date":"2017-05-07T15:01:43","date_gmt":"2017-05-07T15:01:43","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1384"},"modified":"2024-11-16T14:56:55","modified_gmt":"2024-11-16T14:56:55","slug":"program-to-generate-and-print-an-identity-matrix","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/program-to-generate-and-print-an-identity-matrix\/","title":{"rendered":"Program to generate and print an identity matrix?"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p>An identity matrix is a square matrix of dimension n which contains ones along the principal diagonal and zeros in the rest of the other positions.<\/p>\n<p>A 2\u00d72 matrix is as follows:<\/p>\n<p>1\u00a0\u00a0\u00a0 0<\/p>\n<p>0\u00a0\u00a0\u00a0 1<\/p>\n<h3>4&#215;4 Identity Matrix Example<\/h3>\n<p>For example, 4\u00d74 matrix is shown below.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1187\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Identity-Matrix-4x4-.png\" alt=\"Identity Matrix 4x4\" width=\"1074\" height=\"601\" title=\"\"><\/p>\n<p>&nbsp;<\/p>\n<h3><strong>Java Program<\/strong><\/h3>\n<p>Please note that Java does not have true multidimensional arrays. For instance, a two-dimensional array is implemented as an array of one-dimensional arrays.<\/p>\n<p>Sample java program to generate and display a matrix of 6\u00d76. It is a\u00a0 straight forward program with two code blocks. The first code block fills the matrix. Fill logic populates with one if the row and column are the same.<\/p>\n<p>The second code block displays the generated matrix.<\/p>\n<h3>Code Listing<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class IdentityMatrix {\r\n    public static void main (String args[]) {\r\n        int[][] imatrix;\r\n        imatrix = new int[6][6];\r\n\r\n        \/\/ fill the identity matrix\r\n        for (int row=0; row &lt; 6; row++) {\r\n            for (int col=0; col &lt; 6; col++) {\r\n                if (row == col) {\r\n                    imatrix[row][col]=1;\r\n                }\r\n                else {\r\n                    imatrix[row][col] = 0;\r\n                }\r\n        }    }\r\n\r\n        \/\/ print the identity matrix\r\n        for (int row=0; row &lt;6; row++) {\r\n            for (int col=0; col &lt; 6; col++)\r\n                System.out.print(\" \" + imatrix[row][col] + \" \");\r\n            System.out.println();\r\n        }\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h3><strong>Output of the program<\/strong><\/h3>\n<p>1 0 0 0 0 0<br \/>\n0 1 0 0 0 0<br \/>\n0 0 1 0 0 0<br \/>\n0 0 0 1 0 0<br \/>\n0 0 0 0 1 0<br \/>\n0 0 0 0 0 1<\/p>\n<p>&nbsp;<\/p>\n<h3>Screenshot<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1188\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Identiy-matrix-java-program.png\" alt=\"Identiy matrix java program\" width=\"1285\" height=\"694\" title=\"\"><\/p>\n<div id=\"themify_builder_content-1185\" class=\"themify_builder_content themify_builder_content-1185 themify_builder\" data-postid=\"1185\"><\/div>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-1384\" data-postid=\"1384\" class=\"themify_builder_content themify_builder_content-1384 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Introduction An identity matrix is a square matrix of dimension n which contains ones along the principal diagonal and zeros in the rest of the other positions. A 2\u00d72 matrix is as follows: 1\u00a0\u00a0\u00a0 0 0\u00a0\u00a0\u00a0 1 4&#215;4 Identity Matrix Example For example, 4\u00d74 matrix is shown below. &nbsp; Java Program Please note that Java [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1384","post","type-post","status-publish","format-standard","hentry","category-automation","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1384","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1384"}],"version-history":[{"count":3,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1384\/revisions"}],"predecessor-version":[{"id":19627,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1384\/revisions\/19627"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}