Ask MATLAB Expert

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

MATLAB, Engineering

  • Category:- MATLAB
  • Reference No.:- M91336406
  • Price:- $35

Guranteed 24 Hours Delivery, In Price:- $35

Have any Question?


Related Questions in MATLAB

Assignment - matlab programmingusing appropriate matlab

Assignment - MatLab Programming Using appropriate MatLab syntax, write the code required to analyse and display the data as per the problem description. The order of the MatLab Program should be as follows: Variables and ...

Assignment details -need to write a code for connecting

Assignment Details - Need to write a code for connecting segments (Lines) a special case of TSP. The problem is to connect lines in 2d/ 3d space with path obstructions. Can you help me write the code for this? Hope you m ...

Assignment -we have daily gridded rainfall data of 40 years

Assignment - We have daily gridded rainfall data of 40 years and structure of the dataset is like below; Lat = [6.5:0.25:38.5]; Lon = [66.5:0.25:100]; Rainfall (135x129x365x40) (Lon, Lat, days, years). Now, we looking fo ...

Question a safe prime is a prime number that can be written

Question : A safe prime is a prime number that can be written in the form 2p + 1 where p is also a prime number. Write a MATLAB script file that finds and displays all safe primes between 1 and 1000.

Question - verify the attached paper with matlab and get

Question - Verify the attached paper with matlab and get all the results in the paper and explain step by step the matlab code. Paper - Improving Massive MIMO Belief Propagation Detector with Deep Neural Network. Attachm ...

Assignment -data is given on which want to do computational

Assignment - Data is given on which want to do computational production planning using Metaheuristic MATLAB Programming: 1) Ant Colony Algorithm on both Partial and Total Flexible Problem. 2) Bee Algorithm on both Partia ...

What comparison of means test was used to answer the

What comparison of means test was used to answer the question

Question 1 manipulate spectral imagehyperspectral images

Question 1. Manipulate spectral image Hyperspectral images can be seen as a generalisation of normal colour images such as RGB images. In a normal RGB colour image, there are 3 channels, i.e. channels for red colour, gre ...

Assignment -matlab codes and simulated model in

Assignment - Matlab codes and simulated model in simulink/matlab and truetime. 1. Matlab codes and simulink model for pid controller optimization using particle swarm optimization (PSO) my plant is integer order 1000/(s^ ...

Assignment matlab programmingusing appropriate matlab

Assignment: MatLab Programming Using appropriate MatLab syntax, write the code required to analyse and display the data as per the problem description. The order of the MatLab Program should be as follows: Variables and ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As