MATLAB Answers

how to end a loop at second last iteration?

36 views (last 30 days)
Khushi Bhatti
Khushi Bhatti on 11 Oct 2018
Commented: Kevin Chng on 11 Oct 2018
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 worked, but it exceeds matrix dimensions. i also used break statement but it didn't if someone knows whats wrong ,feel free to help!
for i=1:length(gFx)
if (d(i)~=0)
if (new_d(i+1)==length(gFx))

  1 Comment

Kevin Chng
Kevin Chng on 11 Oct 2018
new_d(1) = 0;
b = length(d);
new_d(2:1+b) = d;

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 11 Oct 2018
I would do:
d = [0; d];


Sign in to comment.

Sign in to answer this question.