Reshape a given matrix into a specified new shape. If the total number of elements does not match the product of the new dimensions, return a list containing [-1].
def reshape_matrix(a: list[list[int|float]], new_shape: tuple[int, int]) -> list[list[int|float]]:
flat = [elem for row in a for elem in row]
new_rows, new_cols = new_shape
if len(flat) != new_rows * new_cols:
return [[-1]]
return [flat[i * new_cols:(i + 1) * new_cols] for i in range(new_rows)]new_rows * new_cols.new_cols to form each row of the reshaped matrix.