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

2 ビュー (過去 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 件)

Community Treasure Hunt

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

Start Hunting!

Translated by