bigTree
bigTree

Reputation: 2173

Speeding up a code to filter a matrix in python

I have a symmetric matrix d1 of the from [[row1],[row2] etc] from which I want to remove some elements. The indices of the elements I want to remove are indicated in the list ind (eg ind = [1,2,3] means I want to remove d1[1][1],d1[2][2] and d1[3][3]).

I wrote the following piece of code to write the filtered matrix d1 in d2. However, it is taking forever to run. Is there any way to speed up that code?

 for k in range(len(d1)):
     if k not in ind:
         d2.append([])
         for j in range(len(d1)):
             if j not in ind:
                 d2[-1].append(d1[k][j])

 print(d2)
 return d2

example:

d1 = [[1, 2, 3,6,8],[4,5,6,6,6],[7,8,9,6,6],[1, 2, 3,6,6],[1, 2, 3,6,9]]

ind = [0,3]

d2 = [[5,6,6],[8,9,6],[2, 3,9]]

Upvotes: 1

Views: 52

Answers (2)

tobias_k
tobias_k

Reputation: 82899

The main problem probably is that if ind is a very long list (thousands of entries) than each time you do if x not in ind you have to check the entire list. Just changing ind to be a set will probably speed up things a lot. Also, instead of checking the condition in both loops, you could just create a list of good indices to retain. Finally, making it a list comprehension might speed it up still a bit more and make it more readable.

ind_set = set(ind)
retain = [i for i in range(len(d1)) if i not in ind_set]
d2 = [[d1[k][j] for j in retain] for k in retain]

Upvotes: 1

mishik
mishik

Reputation: 10003

I would suggest to start with simple optimization:

good_indices = set(range(len(d1))) - set(ind)

for k in good_indices:
    d2.append([])
    for j in good_indices:
        d2[-1].append(d1[k][j])

print(d2)
return d2

Upvotes: 1

Related Questions