concept
Matrices and tensor shapes
Rectangular arrays have row/column shape. Transposition swaps axes; matrix multiplication requires matching inner dimensions. Broadcasting is a separate array-library rule.
Formal Syntax & Structure
artificial-intelligence syntaxartificial-intelligence
Matrices and tensor shapesWorking Example
Example Snippetartificial-intelligence
def transpose(rows): if not rows or not rows[0]: raise ValueError("Use a nonempty matrix") width = len(rows[0]) if any(len(row) != width for row in rows): raise ValueError("Use rectangular rows") result = [] for j in range(width): column = [] for row in rows: column.append(row[j]) result.append(column) return result
rows = [[2, 3, 1], [1, 0, 4]]print(rows[1][2])print(transpose(rows))