How do I fix index exceeds the number of array elements (4)

6 Ansichten (letzte 30 Tage)
The error is in line 8. I've been stuck on this for so long and I can't figure out what I need to do. I am trying to find the closest point to the current_point and the distance from that point.
Thank you
  1 Kommentar
Stephen23
Stephen23 am 17 Aug. 2020
x has four elements, but you ask for the fifth element.
y has four elements, but you ask for the fifth element.
You need to consider your logic.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Alan Stevens
Alan Stevens am 17 Aug. 2020
Your last index in points_to_check is 5, but you only have arrays with 4 elements. Change the 5 to 4 and you get rid of the error message.
  2 Kommentare
Keegan Harvey
Keegan Harvey am 17 Aug. 2020
Thank you! When I do this however it only gives me one answer when I'm also after the index of the point. I believe this is only the distance
Alan Stevens
Alan Stevens am 17 Aug. 2020
I get the index as well!
>> x = [0 1 2 3];
>> y = [1 2 3 4];
>> cp = 1;
>> ptc = [2 3 4];
>> [nextpoint, d] = min( sqrt( (x(cp)-x(ptc)).^2 + (y(cp)-y(ptc)).^2 ) )
nextpoint =
1.4142
d =
1

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Thomas Fournier
Thomas Fournier am 17 Aug. 2020
Hi, can you discribe the error message?
I can see a problem, i don't know what you want to do but
[next_point, d]= expect 2 variable, and you give him only one
mabe try something like that:
[next_point, d]=[current_point+1,min(sqrt((x(current_point.....etc ]
  1 Kommentar
Keegan Harvey
Keegan Harvey am 17 Aug. 2020
That's what shows when I try and run the function in the command window.
I am trying to determine the index and distance of the closest point (next_point) to the current_point

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by