Loop Analysis in MATLAB: What is the Code Doing?

In summary: Since the for loop runs for 5 iterations, A ends up being 4.In summary, the given code uses a while loop to continuously execute a for loop that subtracts and adds 1 to A, with each iteration of the for loop using a different value for k. The code ultimately results in A being 4.
  • #1
gfd43tg
Gold Member
950
50
Hello,

I am trying to decipher loop codes and I am having a lot of difficulty doing so

Here is an example code
Code:
A = 0;
while A < 1
    for k=1:5
        A = A+(-1)^k;
    end
    A = A+k;
end

So I thought the while function does something an infinite amount of times. This is what I think this code is doing

It starts with A = 0, (not sure what the while A < 1 is doing), then the for statement does this

A = 0 + (-1)^1 = -1
A = -1 + 1 = 0

for k = 1, then it must repeat this statement (k=2). The A = 0 in this line is not from the first line of code, it comes from the result for k=1,
A = 0 + (-1)^2 = 1
A = 1 + 2 = 3

well I guess somewhere here the A < 1 is violated, thus the code should stop? I run it and I get 4. I don't understand how this code is working.
 
Physics news on Phys.org
  • #2
This code is pretty much nonsensical and can be simplified:

the for k=1:5 loop

A = A -1 + 1 -1 +1 -1

which basically subtracts one from A which means the outer while loop will run forever except

the statement A = A + k is outside the loop and so k is undefined at this point ie you should get a compile error

Where did you get this code?
 
  • #3
It was an example code problem in the lesson
 
  • #4
Maylis said:
Hello,

I am trying to decipher loop codes and I am having a lot of difficulty doing so

Here is an example code
Code:
A = 0;
while A < 1
    for k=1:5
        A = A+(-1)^k;
    end
    A = A+k;
end

<snipped>

I run it and I get 4. I don't understand how this code is working.
Step through it line by line and you can see how it gets a result of 4.

1. A = 0.
2. while A<1 is true, so continue.
3. Enter the for loop, and start with k=1.
4. A =-1
5. now k=2.
6. A = 0
7. now k=3.
8. A = -1
9. now k=4.
10. A = 0
11. now k=5, the last time through the for loop.
12. A =-1.
13. A = A+k, so A = -1+5 = 4.
14. Check if A<1. It evaluates to false so exit the while loop.
15. Return the answer of 4.

Basically, a while loop executes as long as the condition is true. In this case, it executes as long as A<1 is true.

A for loop executes for each specified value. So for k=1:5 means the loop executes for each separate value of k. Notice that the A=A+k line doesn't execute until the for loop is completely done.
 
  • #5


Hello,

Thank you for reaching out about your difficulties with deciphering loop codes in MATLAB. I understand that it can be challenging to understand the logic behind a code, especially when it involves loops. Let me explain what this code is doing step by step to help you better understand it.

First, let's look at the while loop. This loop will continue to run as long as the condition "A < 1" is true. In this case, A is initially set to 0, so the loop will run at least once.

Next, we have the for loop. This loop will run 5 times, as specified by the statement "for k=1:5". Inside the loop, A is being updated with each iteration. Let's break down the code inside the for loop:

1. k=1: This sets the value of k to 1 for the first iteration.
2. (-1)^k: This is a mathematical operation that raises -1 to the power of k. For k=1, this results in -1, and for k=2, it results in 1.
3. A = A+(-1)^k: This updates the value of A by adding the result of the previous operation (-1 or 1) to the current value of A.
4. A = A+k: This updates the value of A again by adding the value of k (which is currently 1) to the current value of A.

So, for the first iteration of the for loop, A will be updated to 0 + (-1) + 1 = 0. For the second iteration, A will be updated to 0 + 1 + 2 = 3. This process will continue for 5 iterations, resulting in the final value of A being 3.

Now, back to the while loop. After the for loop has completed, A will be updated again by adding the value of k (which is currently 5) to the current value of A, resulting in A = 3 + 5 = 8. Since 8 is not less than 1, the condition of the while loop is now false, and the loop will stop running.

To summarize, this code is using a while loop to control the overall flow of the code, and a for loop to repeatedly update the value of A. In this case, the code is not running an infinite number of times, but rather 6 times
 

Related to Loop Analysis in MATLAB: What is the Code Doing?

1. How does loop analysis work in MATLAB?

Loop analysis in MATLAB involves using a loop structure, such as a for loop or while loop, to repeatedly execute a set of code. This allows for efficient and concise coding, especially when working with large datasets.

2. What is the purpose of loop analysis in MATLAB?

The purpose of loop analysis in MATLAB is to automate repetitive tasks and perform calculations or operations on multiple sets of data. This can save time and effort when working with large datasets or performing complex calculations.

3. How do I create a loop in MATLAB?

To create a loop in MATLAB, you can use the for loop or while loop structure. The for loop is typically used when you know the number of iterations beforehand, while the while loop is used when the number of iterations is dependent on a condition.

4. Can I use loop analysis in MATLAB for data analysis?

Yes, loop analysis can be used for data analysis in MATLAB. It allows for efficient processing and manipulation of large datasets, making it a valuable tool for data analysis tasks.

5. Are there any tips for optimizing loop analysis in MATLAB?

To optimize loop analysis in MATLAB, it is important to preallocate memory for variables and avoid unnecessary calculations within the loop. It can also be helpful to vectorize operations, which means performing operations on entire arrays instead of individual elements.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
662
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
240
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top