0

I want to make a function that outputs an array of predefined size. The function is dependent on variables x, y, rx, and ry, and d. Variables x and y are directly related to the cartesian values. rx and ry are the radius of the blobs generated from the function.

I have already converted array indiies from the traditional 0, 0 on the upper left corner to the middle most pixel. This array should always be odd in herms of length and width.

The blob below gives me one array at a time. I need a stack of arrays I can add together. When I change x and y, the size of the array changes, but I need the array to be the same size or zeroed out at on the borders.

I have some code that follows:

def equation(x, y):
    return 1 * np.exp(-(x**2 + y**2))


def make_matrix(xs, ys, x_min=nxs, y_min=nys): #makes cartesian values
    out = [] #outputs array
    for i in range(x_min, xs - center_x):
        row = []
        for j in range(y_min, ys - center_y):
            row.append(equation(i, j))
        out.append(row)
    return out

blob = np.asarray(list(np.float_(make_matrix(x, y))))
1
  • Your prose does not match the code at all. I have provided an answer that matches your code, and hopefully shows you the tools that you need. I am unlikely to be able to match the prose portion without a lot of additional information. Commented Jul 15, 2019 at 19:13

1 Answer 1

1

Vectorization is your friend here. No need to compute lists element-by element when your function can be evaluated element-wise for an entire array.

def make_matrix(xs, ys, x_min=nxs, y_min=nys):
    x = np.arange(x_min, xs - center_x).reshape(1, -1)
    y = np.arange(y_min, ys - center_y).reshape(-1, 1)
    out = np.exp(-(x**2 + y**2))
    return out

The reshape operations create a 1xN row vector for x and a Nx1 column vector for y. These shapes ensure that broadcasting in the add (+) operation expands x**2 + y**2 to an array of the final size you want.

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

Comments

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.