create a loop that number adds by 1 in matlab

To create a loop that adds a number by 1 in MATLAB, you can use the for loop or while loop. Here's an example of how to do it using a for loop:

main.m
for i = 1:10
    number = number + 1;
    disp(number);
end
60 chars
5 lines

In this example, the loop starts from 1 and iterates 10 times. Inside the loop, the number variable is incremented by 1 using the number = number + 1; statement. The disp(number); statement is used to display the value of number on each iteration.

If you prefer to use a while loop, you can do it like this:

main.m
i = 1;
while i <= 10
    number = number + 1;
    disp(number);
    i = i + 1;
end
83 chars
7 lines

In this example, the loop continues as long as i is less than or equal to 10. The number is incremented, displayed, and i is incremented by 1 on each iteration.

Note: In both examples, you need to initialize the number variable before the loop starts.

related categories

gistlibby LogSnag