Question: 1. Write a MATLAB function (testPrime.m) that takes a scalar as an input argument and uses a for-loop and rem function to check whether the

1. Write a MATLAB function (testPrime.m) that takes a scalar as an input argument and uses a for-loop and rem function to check whether the argument is a prime number. testPrime(x) should return 1 if x is a prime number and 0 otherwise. Your testPrime function must behave as shown below: (user input is underlined) >> help testPrime testPrime(n) determine whether n is a prime number return true (1) if n is prime return false (0) if n is not prime Author: >> testPrime(12) ans = logical 0

>> testPrime(7) ans = logical 1 Hint: To get a logical 1 and logical 0 as output of your function, see "help true" and "help false".

2. Initialize a vector z with the following values:

49 7 59 87 97 44 99

Using a for-loop, conditional statements, and your testPrime function from the previous step, write a MATLAB program that creates two vectors from x. The first vector (call it P) contains elements of x which are prime numbers, and the second vector (call it N) contains the non-prime elements of x. In both P and N, the elements are in the same order as in x. At the end of the program, display P and N as follows: The prime vector is 7 59 97 The non-prime vector is 49 87 44 99

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!