1

For ease of explanation I will be using 2-dimensional numpy arrays (I am using 3-dimensional arrays in my problem).

I have 2 arrays, one records the odds of something occuring at those specific coordinates. The other array is a pre-generated matrix that is used to lower the values of the first array around a central point in a pre-determined radius.

I want to automatically select points in the first matrix (henceforth A), and prevent the program to select another point that sits too close to previously selected points. So I want to multiply the values around the selected point related to the distance from said point.

E.G.:

Matrix A:

[[0, 0, 0, 0, 0, 0],
 [0, 1, 2, 2, 1, 0],
 [0, 2, 4, 4, 2, 0],
 [0, 2, 4, 4, 2, 0],
 [0, 1, 2, 2, 1, 0],
 [0, 0, 0, 0, 0, 0]]

Matrix B:

[[ 1, 0.5, 1 ],
 [0.5, 0, 0.5],
 [ 1, 0.5, 1 ]]

Now say that index [2, 1] is selected as a point of interest. B is multiplied with A, but only with the values in a 3*3 around [2, 1]

Result:

[[0,  0,  0, 0, 0, 0],
 [0, 0.5, 0, 1, 1, 0],
 [0,  2,  2, 4, 2, 0],
 [0,  2,  4, 4, 2, 0],
 [0,  1,  2, 2, 1, 0],
 [0,  0,  0, 0, 0, 0]]

This should result in the points around [2, 1] not being valuable enough to be selected as points of interest, unless the conditions are high enough to be selected anyway, hence the multiplication.

Now, I can't seem to figure out a way to perform this specific multiplication. numpy.multiply() would repeat B so that it gets applied over the entire matrix A while I only want to apply it on a small part of A.

Another option would be looping over the affected section of Matrix A, but this requires an insane amount of time (especially in 3-dimensional matrices)

In other words, I want to apply a convolution filter without summing up the multiplication results at the end, but apply them to the underlying values of the convolved matrix (A)

Any insights on this issue are appreciated.

6
  • did you try numpy.multiply(A,B) ? Commented Feb 19, 2018 at 21:02
  • @ElMaravilla Yes, the issue with numpy.multiply(), as I added in my edit, is that it will repeat B so that it will fit inside A, thus applying B over the entirity of A, instead of the small part I want to apply it to. Commented Feb 19, 2018 at 21:12
  • Looks like a convolution problem. stackoverflow.com/questions/48871285/proper-numpy-vectorization Commented Feb 19, 2018 at 21:13
  • @hpaulj Convolutions only change one value per operation though, right? If I were to use a convolve with B on A at [2,1] I would just change [2,1] to the value (1 * 0) + (0.5 * 0) + (1 * 0) + (0.5 * 1) + (0 * 2) + (0.5 * 2) + (1 * 2) + (0.5 * 4) + (1 * 4) == 9.5, right? Instead I want to change each value under the "convolution filter" to what they multiply to be, and not sum them up after. Commented Feb 19, 2018 at 21:32
  • 2 solutions: (1) Create a matrix with all zeros and a 1 at the selected point, then convolve with your 3x3 matrix. The result you can apply in a point-wise multiplication with the input matrix. (2) slice the input matrix around the selected point, apply a point-wise multiplication, and write back to the same slice. Commented Feb 19, 2018 at 21:40

1 Answer 1

2

The easiest solution uses slicing:

A[5:8, 8:11] = np.multiply(A[5:8,8:11], B)

What this does is extract from A the 3x3 area around the selected point (here [6,9]), multiply it (element-wise) by B, and write it back at the same location.


Since you talked about convolution, if you want to use that the approach would be to create a matrix M of the same shape as A, but that is zero everywhere except at the selected point. This matrix you can convolve with B, and then multiply with A:

M = np.zeros(A.shape)
M[6,9] = 1
M = scipy.ndimage.filters.convolve(M, B)
A = np.multiply(A, M)

(or something to that extent, I did not test this variant).

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.