Skip to content Skip to sidebar Skip to footer

How Can I Remove Some Entries In A Matrix. I Am Converting From Matlab To Python

Here is a matlab code i would like to convert m=100; n=100 idx_MV = randperm(m*n); Q = ones(n, m); Q(idx_MV(1:round(MV*m*n))) = 0; can i get a python equivalent of this piec

Solution 1:

Maybe, you could try it :

import numpy as np
q = np.ones((2, 1))
q[your_index_function(args)] = 0

and your_index_function(args) must return a same shape array as q with boolean values according to your conditions (like idx_MV behavior?)

Solution 2:

I figured it out. Thanks @Ludovic

m=100
n=100
MV=0.4
idx = np.random.permutation(m*n)
idx2 = idx[1:round(MV*m*n)]
Q.reshape(-1, 1)[idx2] = 0

Post a Comment for "How Can I Remove Some Entries In A Matrix. I Am Converting From Matlab To Python"