Please help solving Using Matlab Write a function with the header: function (Q)
ID: 3667225 • Letter: P
Question
Please help solving Using Matlab
Write a function with the header: function (Q) = myIsprime(M) Which takes a 2-D matrix M as input and returns a matrix Q of the same size with a 'P' in every position where M has a prime number, and an 'X' in every position where M has a non-prime number. Please use nested for-loops to cycle through members of M You may use built-in function isprime to evaluate each element of M. Test Cases: >> M = [2 15 12 6 ; 7 12 18 8 ; 5 1 9 2 ; 12 9 12 0] M = >> myIsprime (M) ans = PXXX PXXX PXXP XXXX >> M = [115 144 50 152 ; 93 142 20 53 ; 157 162 56 132 ; 34 40 43 158] M =Explanation / Answer
program:
function [ ] = myIsPrime( M )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
for i=1:size(M,1)
result=[];
for j=1:size(M,2)
flag=0;
check=M(i,j);
k=2;
while(k<check)
% disp(mod(check,k));
if(mod(check,k)==0)
flag=1;
break;
end
k=k+1;
end
if(flag==0 & check>=2)
result=[result 'P'];
else
result=[result 'X'];
end
end
disp(result);
end
end
Result:
>> myIsPrime(M)
PXXX
PXXX
PXXP
XXXX
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.