I have a matrix containing a set of points:


Is there a way we can retrieve a directional gradient (vector) from this matrix? The vector should be pointing toward the region that contains higher values. As for this example, as you can see, the values that are greater than 100 are mostly toward the left, therefore we can estimate that the vector maybe pointing leftward from right.

I am looking for a formula that is flexible enough such that, even if we change the size of the matrix, we would be able to compute a directional gradient.

My apologies if I am not using the right term for directional gradient, as my math knowledge only goes as far as calculus 2.

EDIT 1: Each row represent an increasing value of y. At the first row, y=1, and at the last row, y=5. Each column represents an increasing value of x. At first column x=1, and last column x=5.

  • $\begingroup$ To do this, you need to have an expression of the function $f=f(x_1,\dots,x_n)$. I can't tell based on what you have which column or row corresponds to the coordinates and which correspond to the function's value. $\endgroup$ – Paul Jun 13 '17 at 12:31
  • $\begingroup$ each row has increasing y, and each column has increasing x., starting with x=1, and y=1 and ending at x=5, y=5 $\endgroup$ – Anuraag Vaidya Jun 13 '17 at 12:32
  • $\begingroup$ So, for example, $f(1,1)=100$, $f(5,1)=10$, etc.? $\endgroup$ – Paul Jun 13 '17 at 12:34
  • $\begingroup$ @Paul yup, that's correct $\endgroup$ – Anuraag Vaidya Jun 13 '17 at 12:35

You can use standard finite difference methods to estimate the derivative in each direction, and therefore determine the gradient. So for example, the gradient at $(3,3)$ can be estimated component-wise with $$\frac{\partial f(x,y)}{\partial x} \approx \frac{f(x+1,y)-2f(x,y)+f(x-1,y)}{1^2}$$ $$\frac{\partial f(x,y)}{\partial y} \approx \frac{f(x,y+1)-2f(x,y)+f(x,y-1)}{1^2}$$

which could be an appropriate algorithm in the interior. So $f_x(3,3)=-20$, and $f_y(3,3)=-7.5$ so the gradient is given by these derivatives as coefficients $\nabla f=-20 \hat{i}-7.5\hat{j}$.

On the boundary, you will have to use something simpler, since some of the above terms will be missing: for example, at $(1,1)$, you can use $$\frac{\partial f(x,y)}{\partial x} \approx \frac{f(x+1,y)-f(x,y)}{1}$$ $$\frac{\partial f(x,y)}{\partial y} \approx \frac{f(x,y+1)-f(x,y)}{1}$$

| cite | improve this answer | |
  • $\begingroup$ Understood! What would be the step after differentiation of all points? Is it possible to generalize a direction from them? $\endgroup$ – Anuraag Vaidya Jun 13 '17 at 12:44
  • $\begingroup$ @AnuraagVaidya the derivatives become the components of the gradient. See above edited to complete example. $\endgroup$ – Paul Jun 13 '17 at 12:49
  • $\begingroup$ Thanks for the help. Is it possible to extract a single or average angle from the gradient? $\endgroup$ – Anuraag Vaidya Jun 13 '17 at 13:17
  • 1
    $\begingroup$ Sure, take the inverse tangent of the ratio of the y component to the x component. $\endgroup$ – Paul Jun 13 '17 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.