I need a bit of help. I have two arrays. Let's say
alpha = np.arange(1,5,1) ==> [1,2,3,4]
beta = np.arange(5,8,1) ==> [5,6,7]
How do I use values alpha and beta, call a function, and assign the function return into an array whose number of rows is the same as the number of elements in beta and whose number of columns is the same as the number of elements in alpha.
matrix_assign = np.zeros_like((alpha, beta), dtype = 'float')
+---+---+---+---+---+
| | 1 | 2 | 3 | 4 |
+---+---+---+---+---+
| 5 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
| 6 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
| 7 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
The function I have is a little bit more complicated so for this purpose, lets make the function do
np.sqrt((alpha-beta)^2)
This is the code I have so far but it doesn't seem to be working. Essentially, i in alpha and j in beta goes into the function, and the value that is returned shall replace the zero in its respective cell.
for i in range(alpha):
for j in range(beta):
matrix_assign[i,j] = fn.sample_function(i,j)
eg when alpha[0] and beta[1] goes into the function, the output 5 is assigned to the cell matrix_assign[1,0]
Thank you
EDIT:
import numpy as np
alpha = np.arange(1,5,1) #==> [1,2,3,4]
beta = np.arange(5,8,1) #==> [5,6,7]
matrix_assign = np.zeros_like((alpha, beta), dtype = 'float')
for i in range(alpha):
for j in range(beta):
matrix_assign[i,j] = np.sqrt((alpha-beta)^2)
why doesn't the above code work? I'm getting the error
for i in range(alpha):
TypeError: only integer scalar arrays can be converted to a scalar index
using range(len(alpha)) also doesn't work. I get the error
matrix_assign[i,j] = np.sqrt((alpha-beta)^2)
ValueError: operands could not be broadcast together with shapes (4,) (3,)
rangeis a python function that expects an integer, not anumpyarray.matrix_assign[i, j] = np.sqrt( (alpha[i] - beta[j]) ^2 )