dimanche 28 février 2021

How to use slice notation in this case?

I am solving a problem in which I am using two large matrices A and B. Matrix A is formed by ones and zeros and matrix B is formed by positive integers.

At some point, I have to update the components of A such that, if the component is 1 it stays at 1. If the component is 0, it can stay at 0 with probability p or change to 1 with probability 1-p. The parameters p are functions of the same component of matrix B, i.e., I have a list of probabilities such that, if I update A[i,j], then p equals the component B[i,j] of the vector probabilities

I can do the updating with the following code:

for i in range(n):
  for j in range(n):
    if A[i,j]==0:
        pass
    else:
        A[i,j]=np.random.choice([0,1],p=[probabilities[B[i,j]],1-probabilities[B[i,j]]])
        

I think that there should be a faster way to update matrix A using slice notation. Any advice?




Aucun commentaire:

Enregistrer un commentaire