2

I need help for my project. I have two arrays wherein I have to multiply the elements of array1 for each elements in array2.

As an example,

pop_i = [[1, 0, 1]
         [0, 0, 1]
         [1, 1, 0]]

r_q = [[3, 5, 2], [5, 4, 3], [5, 2, 2]] 

What I did first is to arrange r_q to become the array that I wanted.

# simply arranging the values by means of transposition or using zip
r_q = [[3, 5, 5], [5, 4, 2], [2, 3, 2]]

What I need to do now is to multiply the elements in r_q with each elements in pop_i, like:

r_10 = [3, 5, 5] * [1, 0, 1]
r_11 = [3, 5, 5] * [0, 0, 1]
r_12 = [3, 5, 5] * [1, 1, 0]

r_20 = [5, 4, 2] * [1, 0, 1]
r_21 = [5, 4, 2] * [0, 0, 1]
r_22 = [5, 4, 2] * [1, 1, 0]

r_30 = [2, 3, 2] * [1, 0, 1]
r_31 = [2, 3, 2] * [0, 0, 1]
r_32 = [2, 3, 2] * [1, 1, 0]

Afterwards, get their sums.

# r_1_sum = [3*1 + 5*0 + 5*1, 3*0 + 5*0 + 5*1, 3*1 + 5*1 + 5*0] and so on...

r_1_sum = [8, 5, 8]
r_2_sum = [7, 2, 9]
r_3_sum = [4, 2, 5]

I am having a hard time multiplying r_q with each elements in pop_i. So far, my code looks like this:

def fitness_score(g, u):
   # arrange resource demand of r_q 
   result = numpy.array([lst for lst in zip(*r_q)])

   # multiply elements in r_q with each elements in pop_i
   for i in range(0, len(result)):
      multiplied_output = numpy.multiply(result[i], pop_i)
   print(multiplied_output)

   for x in in range(0, len(multiplied_output)):
      final = numpy.sum(multiplied_output[x])

But I keep getting answer for the last index in r_q. I think the multiplication part is wrong. Any help/suggestion would be very much appreciated. Thank you so much!

12
  • Do you understand that for i in r_q: is like a for each loop? r_q[i] is not element, i itself is the element (which in this case is a list). Can you paste the error? Commented Jun 4, 2020 at 7:59
  • @Austin IndexError: index 10 is out of bounds for axis 0 with size 3. Still very much new to Python. Any help would be appreciated. Commented Jun 4, 2020 at 8:04
  • Can you use numpy? Commented Jun 4, 2020 at 8:04
  • @ShubhamSharma in what part? multiplication of r_q with pop_i? I am still trying to figure out how to do it. I find it easy if I simply multiply them, but what I am trying to do is to multiply elements in r_q with each elements in pop_i, then get their sum and store them. I am still new to Python so any help would be appreciated and think through. Commented Jun 4, 2020 at 8:09
  • Yes you can use numpy in multiplication of r_q with pop_i. Commented Jun 4, 2020 at 8:10

3 Answers 3

2

Assuming,

pop_i = [[1, 0, 1],[0, 0, 1],[1, 1, 0]]
r_q = [[3, 5, 2], [5, 4, 3], [5, 2, 2]] 

Use:

matrix = []
for row in zip(*r_q):
    temp = []
    for col in zip(*pop_i):
        temp.append(sum([x*y for x, y in zip(row, col)]))
    matrix.append(temp)

r_1_sum, r_2_sum, r_3_sum = matrix

Or, better use the numpy approach,

import numpy as np

a1 = np.array(pop_i)
a2 = np.array(r_q)
a = a1 @ a2
r_1_sum, r_2_sum, r_3_sum = a.T.tolist()

Result:

[8, 5, 8] # r_1_sum
[7, 2, 9] # r_2_sum
[4, 2, 5] # r_3_sum
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you so much! It worked! Just a follow up question, if you wouldn't mind, would it be possible to loop r_3_sum into r_sum{} = a.T.tolist()? This is because r_sum is highly dependent on the number of activities. Let's say I only have 2 activities, therefore there can only be 2 r_sum. [r_sum_1 and r_sum_2]
I'm not sure i understand your follow up question? Can you explain more?
Instead of r_1_sum and so on, would be alright if i just use r_sum = a.T.tolist(). In this way, answers would be stored in just one list, am i right?
@Acee Yep, That would be totally fine.
Thank you so much! I've been on this part of my code for two days already! Your answer really help me a lot! Very much appreciated!
|
0

You can get the desired result by using numpy dot function.

import numpy as np

pop_i = np.array([[1, 0, 1],[0, 0, 1],[1, 1, 0]])

r_q = np.array([[3, 5, 2], [5, 4, 3], [5, 2, 2]])

result = np.dot(np.transpose(r_q), pop_i)

Refer numpy.dot documentation.

1 Comment

Hello! I tried doing this already but I did not get the result I am expecting. Thank you, still, for answering!
-1

Sample code from the link for reference:

# Program to multiply two matrices using nested loops
# 3x3 matrix
X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]
# 3x4 matrix
Y = [[5,8,1,2],
    [6,7,3,0],
    [4,5,9,1]]
# result is 3x4
result = [[0,0,0,0],
         [0,0,0,0],
         [0,0,0,0]]

# iterate through rows of X
for i in range(len(X)):
   # iterate through columns of Y
   for j in range(len(Y[0])):
       # iterate through rows of Y
       for k in range(len(Y)):
           result[i][j] += X[i][k] * Y[k][j]

for r in result:
   print(r)

Check if this link helps you: https://www.programiz.com/python-programming/examples/multiply-matrix. (It doesn't have the direct answer, but uses the same logic that you are trying.)

1 Comment

Will try to think through the link you sent! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.