Write the matlab code for nested for loops

Assignment Help MATLAB Programming
Reference no: EM13766636

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).

Reference no: EM13766636

Questions Cloud

Problems facing the long term care industry today : Discuss some of the problems facing the long term care industry today. What are some possible solutions for these problems?
Strategic alliances and human resource management : LGE is one of the leading global companies in the industry. It is composed of five divisions: air conditioning, business solutions, home appliances, home entertainment, and mobile communication.
What is a hypothesis test : What is a hypothesis test? Why do we need to use them to make decisions about relating sample results to the population; why can't we just make our decisions by the sample value?
Evaluation at astro bank and the balanced scorecard : IS/IT project managers use evaluation processes to determine the success of a project. This kind of evaluation is not always a straightforward task. In this Discussion, you will determine evaluation criteria in an example project from a case study..
Write the matlab code for nested for loops : write the MATLAB code for 3 nested for loops. Use the same variable naming scheme as above, and have the following number of iterations
Expression ethical business conduct to mean : What you understand the expression "ethical business conduct" to mean - The arguments for and against seeking to promote ethical business conduct.
Include new thoughts or ideas based on module information : Include new thoughts or ideas based on the module information. This is your reflection/insight which logically would flow from each information point presented
Start in the financial accounting area : Just before you came on board, several new, junior accountants had been hired to start in the financial accounting area, as well as a senior financial accounting analyst. To get the junior accountants off on the right foot, you planned to have a b..
What countries are part of the golden crescent : What countries are part of the Golden Triangle? What countries are part of the Golden Crescent? What is the link between the business of doing drugs and the production of drugs in these countries to the United States

Reviews

Write a Review

MATLAB Programming Questions & Answers

  Write a matlab program that produces the sum of square

Write a Matlab program that produces the sum of square of all the even numbers and cube of all the odd numbers between 1 to 100.

  Evaluate the average power for the signals

Evaluate the average power for the signals and graph the discrete-time functions. Find and to graph the unit-sequence response of the system

  Compute the eigenvalues and eigenvectors of the matrix

Compute the eigenvalues and eigenvectors of the matrix

  Determine the average heat transfer coefficients over l 2

water at an average temperature of 47 ordmc and with a flow rate of 0.02 kgs flows through a 2 cm diameter tube which

  Will compute the value of each piece of clothing.

write a script in MATLAB that will compute the value of each piece of clothing.

  Perform gauss-jordan elimination

Write a matlab function to perform gauss-jordan elimination with pivoting. Modify the pivoting so that it is using the row with the highest absolute value rather than the first non-zero row.

  Plot the lines using the inputted vector of x values and x

Write a function named solveSystem that has three inputs: two vectors consisting of coefficients [a b c] of two line equations of the form ax + by = c and a vector of x values.

  The concentration of medication in the blood declines

After a dose, the concentration of medication in the blood declines due to metabolic processes. The half-life of a medication is the time required after an initial dosage for the concentration to be reduced by one-half.

  Matlab: create a function that will use the secant method

MATLAB: Create a function that will use the Secant method

  Write mass balance equations for the remaining reactors

Write mass balance equations for the remaining 4 reactors. Put the known values in the 5 equations and convert each equation into a form where left hand side contains all the variables and the right hand side the constant term.

  Open a named pipe and to read data from the pipe

Open a named pipe and to read data from the pipe in matlab

  It''s a mechanical engineering project proposal

It's a mechanical engineering project proposal. the program that we using is called "Mat lab". Here are the rules for the project proposal.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd