def f_c_v2(M):
return M / M.sum(1).reshape((M.shape[0], 1))
M.sum(1) calculates the sum along axis 1 (the “horizontal” direction in a 2D array), which means it sums each row individually.M has shape $(n,m)$, then M.sum(1) produces a 1D array of length n (one sum per row)..reshape((M.shape[0], 1)) converts that 1D array into a 2D array of shape $(n,1)$.M (shape $(n,m)$) by a $(n,1)$ array, numpy automatically applies the appropriate row sum to each element in that row.M / [reshaped row sums] performs an element-wise division, matching each row’s elements with its corresponding row sum.M gets divided by M.sum(1)[i].M.sum(1)) and reshaping the resulting array ensures easy broadcasting over columns.M / ….Hence, this function succinctly normalises each row of the matrix to sum to 1, showcasing a classic and powerful use of numpy broadcasting.