Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(MATLAB) (5 points total) Write a script called “hw2q2.m” with sections that: a.

ID: 3713129 • Letter: #

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

Explanation / Answer

PLEASE REFER BELOW CODE

close all
clear all
clc


%a)
val=1;
%using loops to generate given matrix
for i = 1:10 %for column
for j = 1:100 %for row
ip_mat(j,i) = val;
val = val + 1;
end
end

%b)
tic
for i = 1:10 %for column
count = 0;
for j = 1:100 %for each row
%checking each element is prime and updating count of prime for each column
if isprime(ip_mat(j,i))
count = count + 1;
end
end
colPrimes2loops(i) = count;
end
el_time1 = toc;
colPrimes2loops

%c)

tic
for i = 1:10 %for each column
count = 0;
temp = ip_mat(:,i); %copying whole column to temp
test = isprime(temp)==1; %checking prime in whole cloumns
colPrimes1loop(i) = sum(test(:)); %count all primes in columns
end
el_time2 = toc;

colPrimes1loop

%d)
i = 1;
tic
while i < 11 %using while loop for each columns
count = 0;
temp = ip_mat(:,i);
test = isprime(temp)==1;
colPrimes1loopWhile(i) = sum(test(:));
i = i + 1;
end
el_time3 = toc;

colPrimes1loopWhile
%e)
%without using loop and accessing each column separately
tic
colPrimes0loops = [sum(isprime(ip_mat(:,1))) sum(isprime(ip_mat(:,2))) sum(isprime(ip_mat(:,3))) sum(isprime(ip_mat(:,4))) ...
sum(isprime(ip_mat(:,5))) sum(isprime(ip_mat(:,6))) sum(isprime(ip_mat(:,7))) sum(isprime(ip_mat(:,8))) ...
sum(isprime(ip_mat(:,9))) sum(isprime(ip_mat(:,10)))]
el_time4 = toc;

%f)
fprintf('Elapsed time using 2 for loops in part(b) = %f ', el_time1);
fprintf('Elapsed time using 1 for loops in part(c) = %f ', el_time2);
fprintf('Elapsed time using 1 while loops in part(d) = %f ', el_time3);
fprintf('Elapsed time without using loops in part(e) = %f ', el_time4);

PLEASE REFER BELOW OUTPUT


colPrimes2loops =

25 21 16 16 17 14 16 14 15 14


colPrimes1loop =

25 21 16 16 17 14 16 14 15 14


colPrimes1loopWhile =

25 21 16 16 17 14 16 14 15 14


colPrimes0loops =

25 21 16 16 17 14 16 14 15 14

Elapsed time using 2 for loops in part(b) = 0.065053
Elapsed time using 1 for loops in part(c) = 0.014779
Elapsed time using 1 while loops in part(d) = 0.006917
Elapsed time without using loops in part(e) = 0.005014