I have a NumPy matrix C and want create a copy of it cPrime, which has some operation of the original matrix to all non-zero values of it. In the code below, for each non-zero element of C, I multiply by 30 and then add 1:import numpy as npsize = 6C = np.zeros((size,size), dtype=int)C[4][0] = 2C[4][1] = 5C[4][2] = 3C[0][3] = 1C[1][3] = 1C[2][3] = 1C[3][5] = 3cPrime = np.zeros((size, size),dtype=int)for i in range(size):    for j in range(size):        if C[i][j] != 0:            cPrime[i][j] = C[i][j]*30 + 1This code works, but it feels inefficient. I'm about 99% sure that there's a really efficient way to achieve this goal, maybe using a masked array, but I haven't been able to figure it out.
Scaling and incrementing non-zero elements of a NumPy matrix
python;numpy;vectorization
This is a typical use case for numpy.where:cPrime = np.where(C, C * 30 + 1, C)This is about twice as fast as (30 * C + 1) * (C != 0) and generalizes more easily to other conditions.