MATLAB Write a function called problem3 that takes an at mos

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.

Solution

%matlab code

function v=hw3_problem3(A)
B=isprime(A);
[a,b]=size(A);
% quit if size if greater thana 3
if (a > 3)
quit
end
% quit if size if greater thana 3
if (b > 3)
quit
end
  
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 % if statement was not closed
end

% test case
A = [2 2 3; 3 4 5; 6 7 8];
hw3_problem3(A)
%{
output:

ans =
2 2 3 3 5 7
%}

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 el
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 el

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site