0

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,)
    
3
  • range is a python function that expects an integer, not a numpy array. Commented Nov 10, 2020 at 10:01
  • and as for the second error you need matrix_assign[i, j] = np.sqrt( (alpha[i] - beta[j]) ^2 ) Commented Nov 10, 2020 at 10:04
  • Check the edit on my newer answer. Commented Nov 10, 2020 at 10:08

2 Answers 2

1

You code should be:

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.size):
    for j in range(beta.size):
        matrix_assign[i,j] = fn.sample_function(alpha[i], beta[j])

fn.sample_function needs single values of alpha and beta, not the indices. This is very slow, though.

If your fn.sample_function is vectorized, you can do this:

A, B = np.meshgrid(alpha, beta)
matrix_assign = fn.sample_function(A, B)

You can also mash any function to be pseudo-vectorized by using np.vectorize.

A, B = np.meshgrid(alpha, beta)
matrix_assign = np.vectorize(fn.sample_function)(A, B)

But this is just a convenience function that doesn't have any time benefit over for loops.

Sign up to request clarification or add additional context in comments.

1 Comment

I see. Thank you @Daniel F. Not exactly what I was expecting but it still is useful nonetheless. I found my problem to be the size of the matrix_assign array. It turns out its a 1x2 array instead of 3x4. No idea why.
0

In general, this is a ufunc.outer operation. Unfortunately, it only works with ufuncs. Subtraction is a ufunc, but not the rest of your op. You can do it this way:

matrix_assign = np.abs(np.subtract.outer(alpha, beta))

But in a generalized way, you'll need to make sure your fn.sample_function is either made up of, or tuned into (as a custom ufunc or a np.frompyfunc), only ufunc.

4 Comments

Hello @Daniel F, thanks for the answer. Hmm, is there a way to make mine work using matrix_assign[i,j] = .... This was how I was taught to assign values to a cell in an array. What exactly am I doing wrong? Thank you
Well, you can np.vectorize it. It won't be any faster than your for loop though.
But unless the internals of fn.sample_function are already vectorized there's not an easy way, no. I suppose you don't want to share that function.
Hmm I see. Well the function is quite long. This section of code is a small part of a bigger code that works by calling functions from different files, so there's too much to share. I'm just trying to get a better understanding of why the code above doesn't work. I know I'm on the right track but I feel like I'm missing something.

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.