Calculate the mean of a matrix either by row or by column, specified by a mode parameter ("row" or "column").
def calculate_matrix_mean(matrix: list[list[float]], mode: str) -> list[float]:
if mode == "row":
return [sum(row) / len(row) for row in matrix]
elif mode == "column":
num_cols = len(matrix[0])
num_rows = len(matrix)
return [sum(matrix[r][c] for r in range(num_rows)) / num_rows for c in range(num_cols)]