Task

Build an   identity matrix   of a size known at run-time.



An identity matrix is a square matrix of size n × n,

where the diagonal elements are all 1s (ones),

and all the other elements are all 0s (zeroes).







I

n


=


[



1


0


0


⋯


0




0


1


0


⋯


0




0


0


1


⋯


0




⋮


⋮


⋮


⋱


⋮




0


0


0


⋯


1



]




{\displaystyle I_{n}={\begin{bmatrix}1&0&0&\cdots &0\\0&1&0&\cdots &0\\0&0&1&\cdots &0\\\vdots &\vdots &\vdots &\ddots &\vdots \\0&0&0&\cdots &1\\\end{bmatrix}}}





Related tasks

   Spiral matrix
   Zig-zag matrix 
   Ulam_spiral_(for_primes)


 
build_matrix(7)
 
Sub build_matrix(n)
	Dim matrix()
	ReDim matrix(n-1,n-1)
	i = 0
	'populate the matrix
	For row = 0 To n-1
		For col = 0 To n-1
			If col = i Then
				matrix(row,col) = 1
			Else
				matrix(row,col) = 0
			End If	
		Next
		i = i + 1
	Next
	'display the matrix
	For row = 0 To n-1
		For col = 0 To n-1
			If col < n-1 Then
				WScript.StdOut.Write matrix(row,col) & " "
			Else
				WScript.StdOut.Write matrix(row,col)
			End If
		Next
		WScript.StdOut.WriteLine
	Next
End Sub
 