A
A
Andrei1penguin12021-02-14 12:22:44
numpy
Andrei1penguin1, 2021-02-14 12:22:44

Which numpy function to choose?

Good day, there is a task for each element of the array to calculate the gradient in x and y:
Example:
There is a matrix

[10, 20, 30],
[15, 25, 35],
[20, 40, 60]

Accordingly, for the number 25 (in the center), the x gradient will be equal to |35-15| = 20, and for y |20-40|=20
You can implement it yourself in several lines, but maybe there are some functions in numpy that allow you to do this faster?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andy_U, 2021-02-14
@Andrei1penguin1

Well then something like this:

import numpy


if __name__ == '__main__':

    a = numpy.array()

    xdiff = numpy.zeros_like(a)
    xdiff[:, 1:-1] = a[:, 2:]-a[:, :-2]
    print(xdiff)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question