-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtranspose.go
More file actions
25 lines (21 loc) · 614 Bytes
/
transpose.go
File metadata and controls
25 lines (21 loc) · 614 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package underscore
// Transpose flips a matrix over its diagonal, swapping rows and columns.
// Returns an empty slice if the input is empty.
// Assumes all rows have the same length (uses the length of the first row).
//
// Example: Transpose([[1,2,3], [4,5,6]]) → [[1,4], [2,5], [3,6]]
func Transpose[T any](matrix [][]T) [][]T {
if len(matrix) == 0 || len(matrix[0]) == 0 {
return [][]T{}
}
rows := len(matrix)
cols := len(matrix[0])
result := make([][]T, cols)
for i := range result {
result[i] = make([]T, rows)
for j := range matrix {
result[i][j] = matrix[j][i]
}
}
return result
}