Computer science

profileiat

1) Nested for-loops are when you have one (or more) additional loops inside of a for-loop. Consider the following MATLAB code:

 

            for i = [1:2];

                        for j = [1:2];

                                    fprintf(‘i is: %d, j is: %d\n’,i,j);

                        end

            end

 

Let’s examine the first line of code. It is simply a for loop that loops twice, storing the current iteration in the variable i. The second for loop is the nested loop. This means that for every one iteration in the outer loop (the i loop), the inner loop (the j loop) will loop until completion, in this case, the j for loop will loop two times for every one i loop. This means that fprintf statements will print the following:

 

            i is: 1, j is: 1

            i is: 1, j is: 2

            i is: 2, j is: 1

            i is: 2, j is: 2

 

The end statement on the last two lines end each for-loop block.

 

 

 

 

 

 

 

We are not limited to just two loops either. The following is perfectly valid:

 

            for i = [1:2];

                        for j = [1:2];

                                    for k = [1:2]

                                                fprintf(‘i is: %d, j is: %d, k is: %d\n’,i,j,k);

                                    end

                        end

            end

 

This means that for every j iteration, k will iterate twice. The outputs now look something like this:

 

            i is: 1, j is: 1, k is: 1

            i is: 1, j is: 1, k is: 2

            i is: 1, j is: 2, k is: 1

            i is: 1, j is: 2, k is: 2

            i is: 2, j is: 1, k is: 1

            etc….

 

Notice the pattern?

 

            a) Given the information above, write the MATLAB code for 3 nested for loops. Use the same variable naming scheme as above, and have the following number of iterations:

 

            i (the outer loop) loops from 1 to 2

            j (the second loop) loops from 1 to 3

            k (the inner most loop) loops from 1 to 2

 

 

 

 

 

           

2) Comparisons can easily be done between components of vectors. Assume we have two vectors: x = [1 2 3], y = [6 4 2]. If we wanted to check if the first element of x is larger than the second element of y, we could write the following code:

 

            if x(1) > y(2); %This is false because x(1) is 1 and y(2) is 4

 

This is pretty straightforward. Similarly, if we wanted to see if the second element of x is equal to the third element of y, we could write the following:

 

            if x(2) == y(3); %This is true because x(2) = 2 and y(3) = 2

 

Now what if we wanted to check multiple things? Maybe we only want to execute certain code if both the above statements are true. This is where logic operators come into play. The following code checks to see if both statements are true. If they are, then the code inside the if-block will execute. If even just one statement is false, then the code does not execute. The following code is an example:

 

if x(1) > y(2) && x(2) == y(2); %This is false.1 is not greater than 4

 

The && is how we specify the logical “and” operator in programming. The above code is false because the first comparison returns false. It does not matter if the second is true at this point. You can also think of it as the following: If Statement 1 is true AND Statement 2 is true, then execute the code in the if-block. However, if either Statement 1 or Statement 2 is false, then we cannot execute the code in the if-block. We can also the check if either statement is true:

 

            if x(1) > y(2) || x(2) == y(2); %This is true

 

Two pipes (the ||) are the symbol for the logical “or” operator in programming. The above is true because even though the first comparison is false, the second is true and the logical “or” only requires one comparison to return true. You can also think of it as the following: If Statement 1 is true OR Statement 2 is true, then execute the code in the if-block.

 

Given the above information, convert the following English statements to a MATLAB logical comparison statement:

 

            a) If the third element of y is less than the first element of x

 

 

b) If the second element of x is equal to y, or the third element of y is less than the third element of x.

 

 

 

 

c) If the first element of x is greater than the second element of y and the second element of x is greater than the third element of y, or the third element of x is greater than the second element of y and the second element of x is greater than the first element of y (hint: Be careful when grouping statements; Use parentheses to separate multiple, logical comparisons).

    • 9 years ago
    • 15
    Answer(1)

    Purchase the answer to view it

    blurred-text
    NOT RATED
    • attachment
      answerforloop.docx