Question: MATLAB: Write a function called problem3 that takes an at most two-dimensional matrix A as its sole input. The function returns a row vector v

MATLAB: Write a function called problem3 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. PLEASE HELP I HAVE POSTED THIS MANY TIMES AND HAVE GOTTEN INCORRECT SOLUTIONS EVERY TIME. IT SHOULD WORK EVEN FOR INPUTS OF 1,2 (ETC.)

The code I have right now is:

function v=hw3_problem3(A) B=isprime(A); [a,b]=size(A); k=0; for i=1:a for j=1:b if (B(i,j)==1) k=k+1; v(k)=A(i,j); end end end if (k==0) display('N0 prime number in given matrix'); end

This works for inputs of 2 and 3 but fails for input of 4, Please help write a new code or by fixing this one. Much appreciated. (The code should work for any input that is within the bounds). It should not display "no prime number in given matrix" rather the code should just not work, please help fix this. Thank you.

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!