Index in position 1 exceeds array bounds (must not exceed 7).

2 vues (au cours des 30 derniers jours)
YAAQOB AL-JAMALI
YAAQOB AL-JAMALI le 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

Réponse acceptée

Karim
Karim le 26 Sep 2022
I indicated in your script where the issue lies, in short:
  • 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.
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];
pathReduced = 7×2
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'
ans = 1×11
6 6 6 6 6 6 6 6 6 6 7
%% 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 commentaires
YAAQOB AL-JAMALI
YAAQOB AL-JAMALI le 26 Sep 2022
Modifié(e) : YAAQOB AL-JAMALI le 26 Sep 2022
Yep, you are right...but how to fix the problem of the consecutive point in P list?
YAAQOB AL-JAMALI
YAAQOB AL-JAMALI le 26 Sep 2022
is it adequate if we change
kk = 1:length(k) to kk=1:length(k)-1

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Linear Algebra dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by