# problem using for loop

조회 수: 1 (최근 30일)
Alfonso Maria Ranieri 2020년 10월 1일
답변: Raunak Gupta 2020년 10월 5일
close all
%key_parameters
a=6396;%startpoint_to_establish_the_lap_modifybytheuser
b=12664;%endpoint_to_establish_the_lap_modifybytheuser
distance=acq.dist(a:b);
Cornersforcycle=zeros(length(distance),1);
Corners=[1861 1920; 1936 2064; 2071 2140; 2161 2272; 2331 2391; 2417 2601; 2645 2791; 2984 3286];
% Corners=[204 340; 379 901; 932 1302; 1389 1972; 2198 2420; 2510 3294; 3455 4066; 4638 5708];
for i=1:length(Corners) <------Is here the error?
for x=1:length(Cornersforcycle) <--------Is here the error?
if distance(x)>=Corners(i,1) && distance(x)<=Corners(i,2)
Cornersforcycle(x)=1;
else
Cornersforcycle(x)=0;
end
end
end
scatter(acq.Track_X(a:b),acq.Track_Y(a:b),5,Cornersforcycle,'filled');
axis auto
grid on
colormap(hsv(2))
colorbar
why the following for loop returns me only the last value? my wish is that the for loop to take the values ​​line by line and not just the last one
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
VBBV 2020년 10월 2일
Use break statement in the else part of _if_loop and see what result you get

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

### 답변 (1개)

Raunak Gupta 2020년 10월 5일
Hi,
Since Corners variable is a matrix, length is probably not the correct function to get the number of elements in particular dimension, you may probably use size. As for the query about last value, I understand that it signifies that only last value in Cornersforcycle is equal to 1. If that is the case, then it is totally dependent on the if condition in the for loop to set the value.
If that doesn’t help, as Vasishta mentioned you can print the intermediate values using disp or can use breakpoints.

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

### 카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by