Replace matrix with new value

Hello, I want to replace the matrix element with the new value.

For example, I have (512,45,1000) matrix.

I also have two tensors (512,1) , (512,1).

In two tensors there are values which are smaller than 45 and 1000 each.

I want to pad the matrix to 0 for the row and column which 0 padding starts with values of each tensor.

now I am using for statement,

for i in range(similarity_score.shape[0]):
similarity_score[i][first_len[i]:]=num
similarity_score[i][:,second_len[i]:] = num

In my thought, it takes much time because of for statement.
Is there any built-in function or more efficient way to reduce the time?