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

3 次查看(过去 30 天)
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

更多回答(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