I have a situation something like this
potential gradient field f (400*600)
[gx, gy] = gradient(f);
I have to find gradient at a point say (590,50). How can I calculate it?Untitled.png

 채택된 답변

Walter Roberson
Walter Roberson 2018년 12월 17일

1 개 추천

[gx(590,50), gy(590,50)]

댓글 수: 7

bilal javed
bilal javed 2018년 12월 17일
But gx and gy are 400*600 matrices. I got this error
Index in position 1 exceeds array bounds (must not exceed 400).
Could you confirm that you have a numeric f, 400 x 600, but you want to find the gradient at a point outside of that grid, at (590, 50) ? Is it possible that you instead want to find at point (50, 590) ?
You need to be careful about what the coordinates refer to . If (590, 50) refer to x and y coordinates, then you need to know how x and y correspond to your rows and columns, with it being likely that x would resolve to a column index, and that y would resolve to a row index. For example, perhaps you have
xvals = linspace(178, 609, 600);
yvals = linspace(-99, 58, 400);
with any particular location f(R,C) corresponding to (xvals( C), yvals( R)) .
If so then to find the values at a bunch of locations, usually it would be easiest to use interp2().
For a single point you could use
[~, cidx] = min( abs(xvals - 590) );
[~, ridx] = min( abs(yvals - 50) );
[ gx(ridx, cidx), gy(ridx, cidx) ]
bilal javed
bilal javed 2018년 12월 17일
Actually I am taking an online course and this is a part of assignment. I wrote in the discussion forum there but didn't find any proper response, just some hints that I don't understand clearly.
here is more about my question
f : A 2D array containing the potential function values.
start_coords : An array specifying the coordinates of the start location the first entry is the x coordinate and the second the y
end_coords : An array specifying the coordinates of the goal the first entry is the x coordinate and the second the y
  1. On every iteration the planner should update the position of the robot based on the gradient values contained in the arrays gx and gy. Make sure you normalize the gradient vectors.
Hints: make sure you're using (x, y) coordinates and (i, j) coordinates correctly
Walter Roberson
Walter Roberson 2018년 12월 17일
For that purpose you can probably assume that the x and y coordinates are integers, and that x is a column number and that y is a row number.
MATLAB arrays are indexed by rows and then columns, and "rows" are considered to correspond to vertical dimension (which corresponds to y in cartesian geometry) and "columns" are considered to correspond to horizontal dimension (which corresponds to x in cartesian geometry.)
bilal javed
bilal javed 2018년 12월 18일
(590, 50) is in xy coordinate, not matrix ij coordinate.
So how can I find the correct value for [gx gy] corresponds to [590, 50]
Walter Roberson
Walter Roberson 2018년 12월 18일
편집: Walter Roberson 2018년 12월 22일
x is a column number .y is a row number . Column 590 row 50 is gx(50,590) because rows are listed first in MATLAB as I explained earlier .
bilal javed
bilal javed 2018년 12월 22일
Thank you Walter Roberson, it really worked

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Mark Sherstan
Mark Sherstan 2018년 12월 17일

0 개 추천

Refer to the post here.

댓글 수: 1

bilal javed
bilal javed 2018년 12월 17일
Thank you!
but my case is little bit different, the matrix f has all numerical values

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2018년 12월 17일

편집:

2018년 12월 22일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by