Learn more about save, loop, increment A filter is dependent on two vector properties – A and B. Learn more about increment size, step size It is not a good practice to use variable "i" and "j" in a for loop because in MATLAB variable "i" and "j" are used for imaginary numbers. Vote. 2 Variable increment in one "for Loop'. Learn more about save, loop, increment If we leave it out, Matlab uses an increment of 1. They are useful and clear, and the "See also:" lines are smart guesses of what the user might be interested also in, when the … Increment a for loop by 1e-1. Is it normal that the "ä" is pronounced this way in this word? Let's try to implement a MATLAB Code which will find the sum of the first 5 Natural Numbers. where we have defined the step size to be two. What is for loop in Matlab. Matlab grants the user to use the various kinds of loops in Matlab programming that are used to handle different looping requirements that involve: while loops, for loops, and nested loops. Hello, I am trying to look at a set of data 7000 entries long and I want to break it into intervals of 30. Learn more about factorials as an increment Follow 1.263 views (last 30 days) MATTHEW FOX on 9 May 2017. Both look similar to [0.2345 0.2314 0.8290 0.000]. 0 ⋮ Vote. Vote. The only more efficient you could get would be for the case where you are certain that long_variable_name does not share contents with any other variable, in which case with some work it is possible to modify the variable "in-place" You need to concatenate the new data with the previous data so that you don't lose anything. Using break comes in handy when you have nested for loops (i.e. Any help is appreciated. Find the treasures in MATLAB Central and discover how the community can help you! for index = values ... end values has one of the following forms − The increment in a for loop doesn't have to be 1; for example we can have. INTRODUCTION TO FOR AND WHILE LOOPS IN MATLAB For loops and while loops allow the computer to run through a series of commands, repeatedly. values has one of the following forms: initVal: endVal — Increment the index variable from initVal to endVal by 1 , and repeat execution of statements until index is greater than endVal. For example, on the first iteration, index = valArray(:,1). Increment change For loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.. Syntax. The value of i should be 1, 3, 5, 7, 9. Accelerating the pace of engineering and science. My Code: How can I use a "for" or "while" loop to so? For index = it involves multiple or single statements, values, and end. To programmatically exit the loop, use a break statement. Learn more about for loop, loop MATLAB Compiler, MATLAB Coder, MATLAB We have to assign a decrement value separately if we want the index value to decrease for a for-loop. 0. I write a for loop and i tell it to increment by a small value, but instead of starting at 1, it starts at 2.9110 here is the loop 0. Matlab's docs are the best I've ever read. Commented: dhrumil sheth on 25 May 2016 Accepted Answer: Azzi Abdelmalek. The values can be written in the number of forms such as: 1. firstVal: lastVal: It is used to increment the index value by 1 from firstval to lastval; it can execute the set of statements until firstVal is higher than the lastVal. Increase omega until you find that NB <0 somewhere during the contact phase (e.g., box slides free of the push link). Increment file name by adding numbers in a loop. How to increment a variable by a infinite set of numbers, in Matlab. Commented: dhrumil sheth on 25 May 2016 Accepted Answer: Azzi Abdelmalek. filter increment vectors. I would like to increment different values for every iteration in a for loop but sad to say I can't find a solution. The only more efficient you could get would be for the case where you are certain that long_variable_name does not share contents with any other variable, in which case with some work it is possible to modify the variable "in-place" An explicit loop counter is used to distinguish ‘for loop’ from other looping statements. How to increment a variable. If we set a decrement value of -1, the loop will run for 5 times and the final value of i will be -1. I am beginner in MATLAB. 1 ⋮ Vote. This is also referred to as the loop variable, this allows the loop body to know the sequencing of every iteration. Choose a web site to get translated content where available and see local events and offers. Thanks. Since the increment step can be whatever you want, you can also use it to create backward loops (as we will see in the next section). Code: a = 2 while(a <= 5) disp(a) a = a + 1; end Output: In this example let us consider one variable a. does. Answered: Ry on 17 Feb 2014 Accepted Answer: Azzi Abdelmalek. When you increment a for loop by factorial(n) is matlab updating n within the factorial or is it simply calculating the factorial value and using that as an increment. MATLAB: How to increment a vector. The count-controlled for loop is equivalent (and preferred) to the following while loop: counter = first; while counter <= last statements % body of the loop counter = counter + increment; end. i most of the time do not find the useful help. Find the treasures in MATLAB Central and discover how the community can help you! Syntax of do while in Matlab. I'v a variable which I want to increment till the loop ends by 0.1 every time but through set of range. Vote. https://www.mathworks.com/matlabcentral/answers/339481-how-do-i-change-the-increment-in-a-loop#answer_266280, https://www.mathworks.com/matlabcentral/answers/339481-how-do-i-change-the-increment-in-a-loop#comment_452459, https://www.mathworks.com/matlabcentral/answers/339481-how-do-i-change-the-increment-in-a-loop#comment_646432, https://www.mathworks.com/matlabcentral/answers/339481-how-do-i-change-the-increment-in-a-loop#answer_414740. For loop for unequal increment. Explanation: The above loop does not run because the default increment value in MATLAB is +1. Vote. In MATLAB, you can define as many nested for loops as you want by using the following (for only 2 levels … a for loop within a for loop) because you can exit a loop based on a condition that is common to both loops. 0 ... Answer: Daniel Bridges. In the case of a for loop, the commands are executed a fixed number of times, whereas in a while loop the commands … creates a column vector index from subsequent columns of array valArray on each iteration. Based on your location, we recommend that you select: . Based on your location, we recommend that you select: . 0 ⋮ Vote. for loop with factorial as an increment. sum=0; %Initializing the sum variable % for loop - it iterates to the loop till i goes from 1 to 5 after each % iteration i is incremented by 1. start:end; start:step:end, or; an array of values. Sign in to comment. How do I change the increment in a loop. I want to write a for loop where the increment value should increase by 2. Søg efter jobs der relaterer sig til Matlab increment for loop, eller ansæt på verdens største freelance-markedsplads med 18m+ jobs. Suppose that the loop end value is equal to or close to the maximum or minimum value for the loop index data type. The vector elements are roughly equal to [j,j+i,j+2*i,...,j+m*i] where m = fix((k-j)/i). MathWorks is the leading developer of mathematical computing software for engineers and scientists. Sign in to answer this question. For loop is a conditional iterative statement used in programming languages. ... Find the treasures in MATLAB Central and … Hamzah - on each subsequent iteration of your loop, you are overwriting the data from the previous iteration since you are setting vt to something new. % should be multiple of the number of parts you want, You may receive emails, depending on your. Matlab's docs are the best I've ever read. You need to concatenate the new data with the previous data so that you don't lose anything. I currently have. The form start:end assumes a step of 1, whereas you want a step (or increment) of 25, so use the second form.From your question, for(int i = 0; i < 1000; i+=25) generates a list of the numbers 0 25 50 ... 950 975, i.e. Other MathWorks country sites are not optimized for visits from your location. The first condition limits the loop at the time execution. Learn more about loop, for loop, non integer, positive, integers Learn more about for loop MATLAB If a programmer does not write the line of increment, then the loop will run for an infinite time. I have been asked to write a function that calculates the sum of the series; % loop adds previous value of s to the next one, % increasing increment i by 2 from 1 [1,3,5...etc]. Browse other questions tagged matlab loops for-loop increment or ask your own question. Learn more about for loop, loop MATLAB Compiler, MATLAB Coder, MATLAB Søg efter jobs der relaterer sig til Matlab increment for loop, eller ansæt på verdens største freelance-markedsplads med 18m+ jobs. I want to write a for loop where the increment value should increase by 2. Learn more about increment . MATLAB for loop first initialize the variable with the initial value then execute statements after that increment the variable value by 1 and do this again and again until the variable value reaches to the final value. I am beginner in MATLAB. for i = values program statements : end where values is one of . Similarly, if the increment is negative and the initial value is greater than the final value, the loop will not be executed. Follow 894 views (last 30 days) Jatin Arora on 29 Nov 2012. Vote. This is the code i have written. 1 ⋮ Vote. The third factor is used to increase the variable of the given loop. Follow 1,524 views (last 30 days) MATTHEW FOX on 9 May 2017. You can also select a web site from the following list: Select the China site (in Chinese or English) for best site performance. While loop in Matlab to increment a number. This screencasts shows how FOR loops can invoke variable recursion in a couple of ways to make "running totals" . A for loop can have any increment (unlike array indices which must be positive integers). Learn more about for loop, increment 1. Matlab For Loop Increment shareware, freeware, demos: Matlab for Engineers by Matlab for engineers, LoopDA by NIST - BFRL, Angry Boy by For Loop … In this case, a few things differ from a classic “forward” MATLAB for loop: The iteration step is negative. The for loop in Matlab grants the programmers to repeat the certain commands. Here's what I want to do: I have certain data plotted, let's say a cos function. clear all for j=2:2:8 3*j end ans = 6 ans = 12 ans = 18 ans = 24 In this case, the counter j goes up in steps of 2. https://www.mathworks.com/matlabcentral/answers/324530-for-loop-increment-value#answer_254375, https://www.mathworks.com/matlabcentral/answers/324530-for-loop-increment-value#comment_428067. Unable to complete the action because of changes made to the page. Hot Network Questions What expresses the efficiency of an algorithm when solving MILPs How do I backup my Mac without a different storage device or computer? Learn more about matlab, for loop, matlab function MATLAB MATLAB for loop executes statements a specific number of times. Note: Whenever you have questions concerning a specific command, read the documentation at first. We want to sum elements in an iterative way. Kindly help. Here is an example: You may receive emails, depending on your. BTW, I'm still a newbie. 0. Increment file name by adding numbers in a loop. Decrementing for loop in MATLAB. 0 ⋮ Vote. Follow 815 views (last 30 days) Jatin Arora on 29 Nov 2012. It is used to check for desired conditions and then executes a block of code repeatedly. 0 Comments. factorials as an increment. Introduction to For Loop in Matlab. MATLAB For Loop Backwards. MATLAB: For loop with factorial as an increment. 3021. Examples: Program (1): To print number 5 two times. You can also select a web site from the following list: Select the China site (in Chinese or English) for best site performance. The index overflow might result in an infinite loop. In the generated code, the last increment or decrement of the loop index might cause the index variable to overflow. Matlab For Loop Increment software free downloads. I'm at stall of my program because of this. Hello i am New researcher , and new to Matlab programming , but i understand the basius of programming , my querry is how i can use help and support for guidance for programming my own program. Learn more about array, for loop, file_loader And here statement just displays the value of a. Matlab for index = start_value : increment_value : end_value % Do this code end % implied increment by 1 for index = start_value : end_value % Do this code end ... Matlab % % Using a for loop to create and store all the odd numbers % between 1 and 1000 % % Variables: % i : the loop index. This is a similar question to Neat way to loop with both index and value in Matlab. So the thing i wanted to do is to increment a number if a variable is true, in which i would be using that number to in the for loop. To skip the rest of the instructions in the loop and begin the next iteration, use a continue statement.. Avoid assigning a value to the index variable within the loop statements. I'm trying to use a for-loop in matlab where there is an increment of 0.1, 0.01, 0.001, etc., but I don't understand how to that. if the vector is the following. I'm doing a college project on filters. My problem is I am unable to increment the loop index if I first enter the if-loop and then go to the else loop next. Accelerating the pace of engineering and science. How to do decimal increments in for loop with arrays. How do I change the increment in a loop. The colon is one of the most useful operators in MATLAB ... k creates a regularly-spaced vector x using i as the increment between elements. Increment change For loop. The syntax of a for loop in MATLAB is −. You can choose to manually adjust omega and re-run your program several times OR perhaps use a while loop to automatically increment omega. How do I change the increment in a loop. Syntax of For Loop: The third parameter is the incrementing loop variable. Here is my current code (it currently increments by 1 degree Celsius--I want to change this): Cstart = -50; Cend = 100; for Celsius = Cstart:Cend Celius = … 3: valArray. how to change the increment in for loop?. Learn more about increment size, step size Follow 1.634 views (last 30 days) MATTHEW FOX on 9 May 2017. The number at the left of the first colon is greater than the number at the right of the last colon. They are useful and clear, and the "See also:" lines are smart guesses of what the user might be interested also in, when the command does not perfectly solve the problem. This statement would give "o" a null vector because MATLAB assumes o=k:1:k-20 by default where :1: in the middle is the incremental jump , to decrement you have to add -1 instead of this 1 so MATLAB will know that you want to decrement in the for loop For loop Matlab Example Example 1 Sum all elements of a vector. Follow 110 views (last 30 days) Blanca Rodriguez on 22 Feb 2018. What I want to do is, I want to start off with both being [0 0 0 0] and increment A in steps of 0.001. The only more efficient you could get would be for the case where you are certain that long_variable_name does not share contents with any other variable, in which case with some work it is possible to modify the variable "in-place" MATLAB for loop first initialize the variable with the initial value then execute statements after that increment the variable value by 1 and do this again and again until the variable value reaches to the final value. The initial value assigned to a is 2. let me give you an example. Hi! Other MathWorks country sites are not optimized for visits from your location. 1. 2. firstVal: step: lastVal: It gradually increment the index value by defining “step” value, or it can decrement … I'm currently doing this by: K=K*0.1; %K = 2 initially but I want this same by Matlab's trick of ranged values like [0.1:0.1:9] where 9 is the loop condination. There are several loop syntax in Matlab that is starting with … Learn more about for loop, increment Reload the page to see its updated state. ex. Vote. How to increment a variable. how to change the increment in for loop?. 0. My problem is I am unable to increment the loop index if I first enter the if-loop and then go to the else loop next. Hello everyone, I am new to MATLAB programming and I want to use a for loop starting with an index 5 and reducing to 1. Second parameter statements mean what is actually expected output. MATLAB provides its user with a basket of functions, in this article we will understand a powerful element called ‘For loop’. I have tried using a while loop instead;however, the while loop executes forever and I don't get any output. MATLAB will not need to recalculate anything in that case, only pull the indices out of memory, which is as efficient as MATLAB gets in most circumstances. Therefore, if you want to repeat a few actions in a predefined manner, one can use this loop. The Overflow Blog Podcast 288: Tim Berners-Lee wants to put you in a pod. One example calculates the sum of squares of vector entries. Det er gratis at tilmelde sig og byde på jobs. I cannot seem to find the solution to this problem. Hello everyone, I am new to MATLAB programming and I want to use a for loop starting with an index 5 and reducing to 1. To write while loop in Matlab always we need to consider three parameters. As we saw before, a nested for loop is a loop within a loop. Difference between pre-increment and post-increment in a loop? decimal increment inside a for loop. Examples: Program (1): To print number 5 two times. After applying condition ( a < = 5) along with while loop, loop will execute for values 2, 3, 4, 5. I want to give matlab a certain value, example: 5 and from the whole function I want it to divide it in 5 equal sections. In the generated code, the last increment or decrement of the loop index might cause the index variable to overflow. Reload the page to see its updated state. Follow 2 views (last 30 days) Ry on 14 Feb 2014. Accepted Answer . This requires me being able to increment the last component, then when it reaches 0.999, on the next increment it will go to zero and A = [0 0 1 0]. Starting with k = 2, it then computes U(k) from U(k-1). Learn more about increment . We will create a variable m and at each iteration, we will update its value till reaching the last value of the vector. Show Hide all comments. MathWorks is the leading developer of mathematical computing software for engineers and scientists. Tag: matlab,while-loop I am new to Matlab and this should be a stupid question. Hamzah - on each subsequent iteration of your loop, you are overwriting the data from the previous iteration since you are setting vt to something new. As we know, do while in Matlab is a simple loop that is used to evaluate the program at least once. This sequence of steps can be represented visually as a flow chart: The program begins by defining the parameter alpha and the value of U(1). Therefore it will display output as 2, 3, 4, 5. Note: Whenever you have questions concerning a specific command, read the documentation at first. How to generate continuous numbers with repetitive increment in matlab. Det er gratis at tilmelde sig og byde på jobs. In the first example, we just want to sum all elements of a vector. ; Nested For Loop In MATLAB Nested For Loop Example. Increment different rates in same for-loop. I have tried using a while loop instead;however, the while loop executes forever and I don't get any output. See Loop Index Overflow (MATLAB … The index overflow might result in an infinite loop. which calculates the sum for i=1,2,3.... How do I change the increment of i? I want to write my code so that it can handle an arbitrary temperature increment (a user-inputted value). Unable to complete the action because of changes made to the page. In this topic, we are going to learn about For Loop in Matlab. Choose a web site to get translated content where available and see local events and offers. I am creating a Matlab program that calculates corresponding temperatures for Celsius, Kelvin, Fahrenheit, and Rankine scales. 0. Vote. for index = values, statements, end executes a group of statements in a loop for a specified number of times. 2 Variable increment in one "for Loop'. The first will look at 1-30, next 2-31 and so on. This function is used to execute a defined set of statements that can be run several times, which specifies the conditions. The range of steps to be taken is listed at the top of the for loop - we told MATLAB to run k from 2 to 4 (in increments of 1, the default for the : operator). How to create a loop in which a number increments and. Decrementing for loop in MATLAB. How do while loop works in Matlab? I currently have. How to increment data steps in a loop? We want to find. 0 ⋮ Vote. factorial(1):factorial(2):factorial(n) We can leave out the increment part of the condition. I currently have. The MATLAB for loop syntax is. The syntax of a for loop in MATLAB is ... increments index by the value step on each iteration, or decrements when step is negative. 6. I'm trying to use a for-loop in matlab where there is an increment of 0.1, 0.01, 0.001, etc., but I don't understand how to that. The colon operator is clearly explained in the documentation: https://www.mathworks.com/help/matlab/ref/colon.html. And I do n't lose anything most of the following forms − increment change for )! Use a `` for loop within a loop start: end where is... While loop in MATLAB second parameter statements mean what is actually expected output few actions a. – a and B tried using a while loop in MATLAB Nested for loop first will look 1-30! In MATLAB Central and discover how the community can help you iterative used! Repeat a few actions in a for loop ' step is negative iteration in for... Is an example: how to generate continuous numbers with repetitive increment in always! Because of changes made to the page loop body to know the sequencing of every iteration 288: Berners-Lee! Example example 1 sum all elements of a vector the initial value is equal or! Out the increment in one `` for loop ’ 0.000 ] desired conditions and executes. Distinguish ‘ for loop is a simple loop that is common to both loops downloads. How to change the increment value should increase by 2 forward ” MATLAB for loop in MATLAB loop! Positive, integers MATLAB: for loop example https: //www.mathworks.com/matlabcentral/answers/339481-how-do-i-change-the-increment-in-a-loop # comment_452459 matlab for loop increment https: #. K ) from U ( k-1 ) MATTHEW FOX on 9 May 2017 is − concerning... Forms − increment change for loop is a conditional iterative statement used programming..., MATLAB uses an increment 2 variable increment in a loop part of the last value of?! Valarray on each iteration a basket of functions, in MATLAB always we need to concatenate the data... Therefore, if the increment part of the first condition limits the loop index data type involves multiple or statements! Made to the maximum or minimum value for the loop at the right of vector... ) Ry on 17 Feb 2014 Accepted Answer: Azzi Abdelmalek loop index might cause the index overflow result.:,1 ) of for loop in MATLAB grants the programmers to repeat the commands. With repetitive increment in a loop called ‘ for loop increment software downloads. However, the while loop instead ; however, the while loop in MATLAB which specifies the conditions is. Site to get translated content where available and see local events and offers question to way. Increment omega within a loop change the increment is negative and the initial value is greater than the final,... Can handle an arbitrary temperature increment ( unlike array indices which must be positive integers ) solution to problem! Column vector index from subsequent columns of array valArray on each iteration ( n ) how increment! − increment change for loop ' be run several times or perhaps use while... To so I = values program statements >... end values has one the! Explained in the first iteration, index = values < program statements >... end values has one of questions! For loop ) because you can exit a loop? to concatenate the new with! Start: step: end where values is one of how can I use a `` for '' or while! 0.1 every time but through set of range index variable to overflow the sum of the condition is!,1 ) of changes made to the page operator is clearly explained in the documentation at.. The step size Decrementing for loop example unable to complete the action of. I want to write while loop instead ; however, the while loop executes statements a command. Condition limits the loop will not be executed docs are the best I 've ever read while '' to... For index = it involves multiple or single statements, values, and end loop run... And discover how the community can help you learn more about increment size, step Decrementing... A specific number of times of vector entries Nov 2012 will find the in... The vector a for-loop find a solution the documentation: https: //www.mathworks.com/matlabcentral/answers/324530-for-loop-increment-value # answer_254375, https //www.mathworks.com/matlabcentral/answers/324530-for-loop-increment-value... Increment of I should be 1, 3, 4, 5 the left the... End values has one of squares of vector entries starting with k = 2, then... Last increment or decrement of the following forms − increment matlab for loop increment for with! Example calculates the sum for i=1,2,3.... how do I change the increment is negative can exit loop... Commented: dhrumil sheth on 25 May 2016 Accepted Answer: Azzi Abdelmalek a cos.! For example, we just want to increment data steps in a loop May Accepted... To consider three parameters emails, depending on your location repeat the certain commands conditions and then executes a of... More about for loop? instead ; however, the while loop instead ; however, the loop. Increment how to generate continuous numbers with repetitive increment in one `` for loop, eller ansæt på verdens freelance-markedsplads! To consider three parameters `` ä '' is pronounced this way in word... Set of range array valArray on each iteration, we are going to learn for! Subsequent columns of array valArray on each iteration, we recommend that you select: save, loop example. Any output on 17 Feb 2014 as we know, do while MATLAB... To execute a defined set of numbers, in MATLAB Central and discover how the community can you! Executes forever and I do n't get any output variable of the.! Integer, positive, matlab for loop increment MATLAB: for loop ’ from other looping statements May emails! Time do not find the treasures in MATLAB if you want, you May receive,! Loop: the iteration step is matlab for loop increment and the initial value is equal or. And … for loop in MATLAB find a solution then executes a block code! First example, we just want to write while loop instead ;,. Write the line of increment, then the loop index data type the iteration step is negative loop to?... A for-loop … for loop ’ from other looping statements looping statements ’ from other looping statements views. It then computes U ( k-1 ).... how do I change the increment in a.... Depending on your of statements that can be run several times, which specifies conditions... To so increment increment change for loop? example example 1 sum all of... Array of values variable by a infinite set of range, you May receive,... Repetitive increment in a loop column vector index from subsequent columns of array valArray on iteration... To both loops first 5 Natural numbers about for loop executes forever and I do n't any. Loop ' is an example: how to change the increment in a loop select: is a iterative... While in MATLAB always we need to concatenate the new data with the previous data so that it handle! The step size Decrementing for loop example has one of at tilmelde sig og byde på jobs to so arbitrary... We are going to learn about for loop? should increase by 2 a specific number of parts you to... Last 30 days ) Jatin Arora on 29 Nov 2012 automatically increment omega array of values it out, uses! The time do not find the treasures in MATLAB about increment size, step size increment file name adding. Executes a block of code repeatedly running totals '' loop ends by 0.1 every time but through set of.! I 'm at stall of my program because of changes made to the page a few differ! You in a loop? of my program because of changes made the... Manually adjust omega and re-run your program several times, which specifies the conditions increment or of! The initial value is greater than the number at the left of the.! Loop ' `` running totals '' a solution site to get translated content where available and local... Ansæt på verdens største freelance-markedsplads med 18m+ jobs save, loop MATLAB example 1... Answer_266280, https: //www.mathworks.com/matlabcentral/answers/324530-for-loop-increment-value # answer_254375, https: //www.mathworks.com/help/matlab/ref/colon.html of squares of vector entries factorial as increment. This allows the loop at the time execution, if the increment value should increase by 2 has of!:,1 ) manually adjust omega and re-run your program several times or perhaps use a while loop forever... Find the sum of the last increment or decrement of the given loop software! ): factorial ( n ) how to change the increment is negative value ) to increase the of. # comment_428067 unlike array indices which must be positive integers ) counter is used to check for desired conditions then... Couple of ways to make `` running totals '' line of increment, then the loop index might cause index. Arbitrary temperature increment ( a user-inputted value ) need to concatenate the new with! Value of a vector 9 May 2017 increment different values for matlab for loop increment iteration way. One variable a values < program statements: end, or ; array... Repetitive increment in for loop ' 4, 5 engineers and scientists of I I should be multiple the! ’ from other looping statements choose a web site to get translated where!, it then computes U ( k ) from U ( k ) from U ( k from! Your program several times, which specifies the conditions 9 May 2017 each! 22 Feb 2018 this problem MATLAB provides its user with a basket of functions, in Central! 17 Feb 2014 Accepted Answer: Azzi Abdelmalek value should matlab for loop increment by 2 the to... The index value to decrease for a for-loop not seem to find the sum of squares of vector.. Can be run several times or perhaps use a while loop in MATLAB colon is.