0

I want to compare every element of two matrices with the same dimensions. I want to know, if one of the elements in the first matrix is smaller than another with the same indices in the second one. I want to fill a third matrice with the values of the first, but every entry, where my criteria applies, should be a 0. Below I will show my approach:

a = ([1, 2, 3], [3, 4, 5], [5, 6, 7])
b = ([2, 3, 1], [3, 5, 4], [4, 4, 4])

c = np.zeros(a.shape)

for i, a_i in enumerate(a):
    a_1 = a_i
    for i, b_i in enumerate(b):
        b_1 = b_i
        if a_1 < b_1:
            c[i] = 0
        else:
            c[i] = a_1

I am very sorry if i made any simple mistakes here, but i am new to python and SO. I found some posts on how to find entrys that match, but I dont know if there is a solution where could use that method. I appreciate any help, thank you.

lempy
  • 103
  • 8

2 Answers2

0

Personally, I find it easier to go through the matrices just using indices:

a = ([1, 2, 3], [3, 4, 5], [5, 6, 7])
b = ([2, 3, 1], [3, 5, 4], [4, 4, 4])

c = ([0, 0, 0], [0, 0, 0], [0, 0, 0])

for i in range(len(a)):
    for j in range(len(a[0])):
        if a[i][j] < b[i][j]:
            c[i][j] = 0
        else:
            c[i][j] = a[i][j]
            

If you use indices to loop through a and b, you can then access the same location in c.

  • The i index tracks which list you are in (i.e the list at index 0 in a is [1, 2, 3] and the list at index 0 in b is [2, 3, 1]).
  • The j index tracks which element of each list you are on (i.e element 0 in the list at index 0 in a is 1 and element 0 in the list at index 0 in b is 2).

This way you can directly access any element in any of the 3 matrices. Additionally, since every element in c starts as 0 you don't need to reassign it to 0 when the element in a is less than the element in b so you could simplify your if/else statement and just do

if a[i][j] >= b[i][j]:
    c[i][j] = a[i][j]

Hopefully this helps!

tdyer
  • 101
  • 3
0

I really like your solution and the feedback, a big thank you for that!

However, in the meantime I found a solution myself (I will just type the loop I used):

for i, a_i in enumerate(a):
    a_1 = a_i
    c = a_1
    for i, b_i in enumerate(b):
        b_1 = b_i
        c[np.where(c < b_1)] = np.nan

What would you say is the quicker solution to calculate?

lempy
  • 103
  • 8
  • 1
    @tdyer, sorry forgot to tag you – lempy Sep 17 '20 at 17:05
  • your solution looks good! At the end of the day they'll both run in about the same time given that they each use 2 for loops so just go with whatever makes the most sense to you. I won't get into it here, but if you're interested in really optimizing your code in the future I suggest you read up on runtime complexity and how it is calculated. It can be a bit abstract at first but definitely useful once you get the hang of it. Here's a post about it that contains tons of resources and explanation https://stackoverflow.com/questions/11032015/how-to-find-time-complexity-of-an-algorithm – tdyer Sep 18 '20 at 14:36