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

조회 수: 3 (최근 30일)
YAAQOB AL-JAMALI
YAAQOB AL-JAMALI 2022년 9월 26일
댓글: YAAQOB AL-JAMALI 2022년 9월 26일
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

채택된 답변

Karim
Karim 2022년 9월 26일
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
YAAQOB AL-JAMALI
YAAQOB AL-JAMALI 2022년 9월 26일
편집: YAAQOB AL-JAMALI 2022년 9월 26일
Yep, you are right...but how to fix the problem of the consecutive point in P list?
YAAQOB AL-JAMALI
YAAQOB AL-JAMALI 2022년 9월 26일
is it adequate if we change
kk = 1:length(k) to kk=1:length(k)-1

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by