Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Problem with for loop

조회 수: 2 (최근 30일)
Oscar Espinosa
Oscar Espinosa 2018년 11월 15일
마감: MATLAB Answer Bot 2021년 8월 20일
I want to obtain the middle points of each panel of a closed circle, for that I'm using the for loop. For example, the length of xc is 9 thus i = 8, but I get a matrix of dimension 1x9 for xc and yc, where the first element is equal to 0. Why is this happening and how can I obtain xc and yc of dimensions 1x8?
Thanks in advance.
for i = 2:1:length(xp);
i
xc(i) = (xp(i-1)+xp(i))/2;
yc(i) = (yp(i-1)+yp(i))/2;
end
  댓글 수: 1
Adam
Adam 2018년 11월 15일
You start indexing from 2 but you can't have an array with nothing as the first element so that first element is created as 0.
You could just index as
xc( i - 1 )
yc( i - 1 )
in this case.

답변 (1개)

madhan ravi
madhan ravi 2018년 11월 15일
your loop produces eight elements but you already defined the first element for each variable as 0 , if you want to know what i mean type
xc(2) %which is the first element of loop but second element of the vector itself
xc(1) %which will be zero because it's predefined

태그

Community Treasure Hunt

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

Start Hunting!

Translated by