How do I fix index exceeds the number of array elements (4)
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Keegan Harvey
am 17 Aug. 2020
Kommentiert: Alan Stevens
am 17 Aug. 2020

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
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.
Akzeptierte Antwort
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
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
Weitere Antworten (1)
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 ]
Siehe auch
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!

