MATLAB: For this problem, you are prohibited from using explicit loops (i.e., fo
ID: 3864381 • Letter: M
Question
MATLAB:
For this problem, you are prohibited from using explicit loops (i.e., for-loop or while-loop). You must use logical indexing instead. Note that an ifstatement is not a loop and can be used in your solution.
Write a function called problem5 that takes an at most two-dimensional matrix A as its sole input. The function returns a row vector v containing all elements of A that are prime numbers. The elements of v are stored according to row-major ordering of A. You are allowed to use the isprime built-in function.
Explanation / Answer
Program:
function [ primeNumbers ] = problem5( input )
primeNumbers=[];
primeNumbers=input(isprime(input (:,:)));
end
Result:
>>input
input=
15 3 69 67
61 41 52 69
98 6 58741 5413
45 69 87 541
>> problem5(input)
ans =
61
3
41
58741
67
5413
541
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.