# how to end a loop at second last iteration?

조회 수: 14(최근 30일)
Khushi Bhatti 2018년 10월 11일
댓글: Kevin Chng 2018년 10월 11일
I have an array. I want to add zero at its beginning. e.g my array has 761 elements. 'd' is array which has 761x1 dimension in this case. i want to make 762 elements by adding first element as zero. gFx has dimension of 762x1 so i created a new array of zeros having size of gFx. then i apply a for loop.it worked, but it exceeds matrix dimensions. i also used break statement but it didn't work.so if someone knows whats wrong ,feel free to help!
[a,b]=size(gFx);
new_d=zeros(a,b);
for i=1:length(gFx)
if (d(i)~=0)
new_d(i+1)=d(i)
if (new_d(i+1)==length(gFx))
break
end
end
end
##### 댓글 수: 1표시숨기기 없음
Kevin Chng 2018년 10월 11일
Try
new_d(1) = 0;
b = length(d);
new_d(2:1+b) = d;

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

### 답변(1개)

Star Strider 2018년 10월 11일
I would do:
d = [0; d];

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

### Community Treasure Hunt

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

Start Hunting!