Question: (MATLAB) (5 points total) Write a script called hw2q2.m with sections that: a. (0.5 points) Create a 100x10 matrix with all numbers from 1 to
(MATLAB) (5 points total) Write a script called hw2q2.m with sections that:
a. (0.5 points) Create a 100x10 matrix with all numbers from 1 to 1000, so that each set of hundreds is in its own column (first col is 1-100, second is 101-200, etc.).
b. (1 point) Use a for loop to step through each column, and a second (embedded) for loop to step through each row. For each column, count how many numbers are prime (using function isprime on one element at a time). Create a vector called colPrimes2loops to store the number of primes in each column
c. (1 point) In a separate section of code (outside of the loops for part b), use a single for loop to step through each column to count how many prime numbers are in that column (using function isprime on a vector)do not use a second (embedded) for loop in this part. Inside the loop, create a vector called colPrimes1loop to store the number of primes in each column.
d. (1 point) In a third section of code, alter the code from part c to use a while loop instead of a for loop. Repeat the same code inside the loop to create a vector called colPrimes1loopWhile to store the number of primes in each column.
e. (1 point) In a fourth section of code, come up with a way of counting the number of primes in each column without using any loops, and create a vector called colPrimes0loops to store those values.
f. (0.5 points) Use the functions tic and toc to time how long it takes your code for parts b, c, d, and e to run. Which is the fastest? Which is the slowest? Why? (Answer in comments in your script.)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
