- P is a 7x2 matrix.
- when kk == length(k), it means that k(kk) = 7
- Hence when you are try to acces P(k(kk)+1, 1), you try to acces P(8,1) which doesn't exist and hence the error is thrown.
Index in position 1 exceeds array bounds (must not exceed 7).
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
YAAQOB AL-JAMALI
am 26 Sep. 2022
Kommentiert: YAAQOB AL-JAMALI
am 26 Sep. 2022
I am trying to locat the collide segments then add a midpoint between the starting and end point of the collide segment, but I keep getting the message "Index in position 1 exceeds array bounds (must not exceed 7)."
iwant = [284 377; 287 378 ; 291 380; 295 381; 298 382; 302 383; 306 384; 310 385; 314 386; 319 388; 323 389];
pathReduced = [277 37; 326 126; 358 139; 334 166; 222 184; 166 397; 475 405]
PQ = iwant;
P = pathReduced;
count = 0;
%% Find the nearest point for each point in 'collidePoints'relavent to 'referencePoints'
k = dsearchn(P,PQ);
%% Adding a midepoint between the nearest point in pathReduced and the consecutive one
for kk = 1:length(k)
p_nearest = [P(k(kk),1), P(k(kk),2)];
p_nearestCon = [P(k(kk)+1,1), P(k(kk)+1,2)];
midPoint = [round((p_nearest(1,1) + p_nearestCon(1,1))./2),...
round((p_nearest(1,2) + p_nearestCon(1,2))./2)];
idx = k(kk) + 1;
count = count + 1;
pathReducedRefined = [[P((1:idx-1),1),P((1:idx-1),2)];...
midPoint; [P((idx:end),1),P((idx:end),2)]] ;
end
0 Kommentare
Akzeptierte Antwort
Karim
am 26 Sep. 2022
I indicated in your script where the issue lies, in short:
iwant = [284 377; 287 378 ; 291 380; 295 381; 298 382; 302 383; 306 384; 310 385; 314 386; 319 388; 323 389];
pathReduced = [277 37; 326 126; 358 139; 334 166; 222 184; 166 397; 475 405];
PQ = iwant;
P = pathReduced;
count = 0;
%% Find the nearest point for each point in 'collidePoints'relavent to 'referencePoints'
k = dsearchn(P,PQ);
k'
%% Adding a midepoint between the nearest point in pathReduced and the consecutive one
for kk = 1%:length(k)
p_nearest = [P(k(kk) ,1), P(k(kk ),2)];
p_nearestCon = [P(k(kk)+1,1), P(k(kk)+1,2)];
% ^^^^^^^ the problem lies here...
midPoint = [round((p_nearest(1,1) + p_nearestCon(1,1))./2),round((p_nearest(1,2) + p_nearestCon(1,2))./2)];
idx = k(kk) + 1;
count = count + 1;
pathReducedRefined = [[P((1:idx-1),1),P((1:idx-1),2)];midPoint; [P((idx:end),1),P((idx:end),2)]] ;
end
2 Kommentare
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!