Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.



Inserting values into middle of an array
Posted:
Oct 12, 2013 3:50 PM


Hello,
I have a column vector of which the length can vary. What I want to do is insert a certain value in between every second value of this vector. So far I am able to this with this code:
offset = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]; i = ones(1,5) * 5; Ar1=[i];Pos1=2; Ar2=[i];Pos2=3; offset=[offset(1:(Pos11)) Ar1 offset(Pos1:(Pos21)) Ar2 offset(Pos1:end)];
where I insert 5 fives into the original array for the 2nd and 4th positions. However if I continue like this my code will get very long and also since the length of the original array can change I'll be in trouble. Can anyone explain a way to do this by using a loop? I get very confused with the use of loops!



