March 2019
Intermediate to advanced
336 pages
9h 9m
English
An identity matrix is a unit matrix with ones are on the main diagonal and zeros are elsewhere. The following code snippet creates an identity matrix:
///main package has examples shown// in Go Data Structures and algorithms bookpackage main// importing fmt packageimport ( "fmt")// identity method func Identity(order int) [][]float64 {var matrix [][]float64 matrix = make([][]float64, order)var i int for i = 0; i < order; i++ {var temp []float64 temp = make([]float64, order) temp[i] = 1 matrix[i] = temp } return matrix}// main method func main() { fmt.Println(Identity(4))}
Run the following command to execute the preceding code snippet:
go run identity_matrix.go
The output is as follows:
The next section talks about the
Read now
Unlock full access