eye<\/strong><\/a><\/p>\n\n\n\nThe eye<\/em> tool returns a 2-D array with 1<\/strong>‘s as the diagonal and 0<\/strong>‘s elsewhere. The diagonal can be main, upper or lower depending on the optional parameter k<\/em><\/strong>. A positive k<\/em><\/strong> is for the upper diagonal, a negative k<\/em><\/strong> is for the lower, and a 0<\/strong> k<\/em><\/strong> (default) is for the main diagonal.<\/p>\n\n\n\nimport numpy\nprint numpy.eye(8, 7, k = 1) # 8 X 7 Dimensional array with first upper diagonal 1.\n\n#Output\n[[ 0. 1. 0. 0. 0. 0. 0.]\n [ 0. 0. 1. 0. 0. 0. 0.]\n [ 0. 0. 0. 1. 0. 0. 0.]\n [ 0. 0. 0. 0. 1. 0. 0.]\n [ 0. 0. 0. 0. 0. 1. 0.]\n [ 0. 0. 0. 0. 0. 0. 1.]\n [ 0. 0. 0. 0. 0. 0. 0.]\n [ 0. 0. 0. 0. 0. 0. 0.]]\n\nprint numpy.eye(8, 7, k = -2) # 8 X 7 Dimensional array with second lower diagonal 1.<\/code><\/pre>\n\n\n\n<\/span>Task<\/strong><\/span><\/h2>\n\n\n\nYour task is to print an array of size N <\/em><\/strong>X M<\/strong><\/em> with its main diagonal elements as 1<\/strong>‘s and 0<\/strong>‘s everywhere else.<\/p>\n\n\n\nNote<\/strong><\/p>\n\n\n\nIn order to get alignment correct, please insert the line numpy.set_printoptions(legacy=”1.13″)<\/strong> below the numpy import.<\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nA single line containing the space separated values of N<\/em><\/strong> and M<\/strong><\/em>.
N<\/strong><\/em> denotes the rows.
M<\/strong><\/em> denotes the columns.<\/p>\n\n\n\n<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nPrint the desired N <\/strong><\/em>X M<\/strong><\/em> array.<\/p>\n\n\n\nSample Input<\/strong><\/p>\n\n\n\n3 3<\/code><\/pre>\n\n\n\nSample Output<\/strong><\/p>\n\n\n\n[[ 1. 0. 0.]\n [ 0. 1. 0.]\n [ 0. 0. 1.]]<\/code><\/pre>\n\n\n\n<\/span>Solution – Eye and Identity in Python<\/strong><\/span><\/h2>\n\n\n\nimport numpy\nprint(str(numpy.eye(*map(int,input().split()))).replace('1',' 1').replace('0',' 0'))<\/pre>\n\n\n\n