Ninja
Ninja

Reputation: 23

How do I use a for loop to create a new 2D numpy array?

I asked a similar question before but it was worded badly. I figured some stuff out but not yet successful. I'm trying to create a new 2D array by calling a previous function. I want function_2 to do the same calculation as function_1 except in function_2 it's involving arrays rather than single values.

This is what I have:

import numpy as np 

def function_1(A,B):
    A = 10
    B = 2
    ans = A*B
    return ans


def function_2(C,D):

    C = np.array([1,2,3,4,5])
    D = np.array([1,2,3,4,5])

    #here I create a zero array and include some other codes required

    for i in range(C): #each i are A values
        for j in range(D): #each j are B values
            array[i,j] = function_1(C,D)

    return array
print(array)

The above example gives me this:

[[25. 25. 25. 25. 25.]
 [25. 25. 25. 25. 25.]
 [25. 25. 25. 25. 25.]
 [25. 25. 25. 25. 25.]
 [25. 25. 25. 25. 25.]]

But I want it to take every value of C and D to do calculations and give me something like this:

[[1. 2. 3. 4. 5.]
 [2. 4. 6. 8. 10.]
 [3. 6. 9. 12. 15.]
 [4. 8. 12. 16. 20.]
 [5. 10. 15. 20. 25.]]

Thanks

Upvotes: 0

Views: 208

Answers (2)

Parijat Bhatt
Parijat Bhatt

Reputation: 674

Try this.

ans = []
for i in range(1,6):
    a =[]
    for j in range(1,6):
        a.append(i*j)

    ans.append(a)

ans = np.array(ans)
ans

using np.zeros

ans = np.zeros((5,5),dtype=np.int)
for i in range(1,6):
    for j in range(1,6):
        ans[i-1][j-1]=i*j
ans

Upvotes: 1

MartenCatcher
MartenCatcher

Reputation: 2887

There are couple of errors in your code. Let's figure out.

  1. You override the input value with defaults in the beginning of the functions:
def function_1(A, B):
    -> A = 10
    -> B = 2
    ...

Doesn't matter what the params will be passed to the function it will be const. But I don't understand why it 25 in your example and not 20.

  1. You cannot use range function with array. Please check the reference of the function.
for i in range(C): #here
    for j in range(D): #here
        array[i, j] = function_1(C,D)
  1. You passed arrays to function_1
 -> function_1(C, D)

I don't understand how did you get an array of 25. with these errors. But fixed solution should be:

import numpy as np


def function_1(A, B):
    ans = A * B
    return ans


def function_2(C, D):
    a = np.zeros((len(C), len(D)), dtype=int) # because you have to allocate matrix before use
    for idi, i in enumerate(C):
        for idj, j in enumerate(D):
            a[idi][idj] = function_1(i, j)

    return a


C = np.array([1, 2, 3, 4, 5])
D = np.array([1, 2, 3, 4, 5])
array = function_2(C, D)
print(array)

And the better solution without functions

import numpy as np

C = np.array([1, 2, 3, 4, 5])
D = np.array([1, 2, 3, 4, 5])

diag = np.diag(D)
rows = np.array([C, ] * 5)

print(np.dot(diag, rows))

Both solutions product:

[[ 1  2  3  4  5]
 [ 2  4  6  8 10]
 [ 3  6  9 12 15]
 [ 4  8 12 16 20]
 [ 5 10 15 20 25]]

Upvotes: 1

Related Questions