Cody Problem 14. Find the numeric mean of the prime numbers in a matrix.

조회 수: 3 (최근 30일)
Hidd_1
Hidd_1 2021년 3월 31일
답변: Abhinav 2023년 4월 26일
I found difficulties with the 14th Problem on Cody here is my solution:
function out = meanOfPrimes(in)
[a,b]=size(in)
sum = 0;
for i = 1:a
for j = 1:b
if isprime(in(i,j))
in(i,j) = str2num(in(i,j))
sum = sum + in(i,j);
end
end
end
out = sum/2;
end
Unfortunately it doesn't work, can anyone help me with that!
Thanks in advance.
  댓글 수: 1
Geoff Hayes
Geoff Hayes 2021년 3월 31일
Hidd_1 - why do you do the following
in(i,j) = str2num(in(i,j))
Isn't the in array already numeric? Won't this fail if already numeric? What is your intention here?
Also, try to avoid creating variables whose name matches those of in-built MATLAB functions. In this case, that would be sum.

댓글을 달려면 로그인하십시오.

채택된 답변

David Hill
David Hill 2021년 3월 31일
Input is not a string.
function out = meanOfPrimes(in)
[a,b]=size(in)
sum = 0;
c=0;
for i = 1:a
for j = 1:b
if isprime(in(i,j))
%in(i,j) = str2num(in(i,j))
sum = sum + in(i,j);
c=c+1;%need to keep track of how many
end
end
end
out = sum/c;%mean is sum of primes/number of primes
end
Alternatively, linear index into matrix
function out = meanOfPrimes(in)
sum = 0;
c=0;
for i = 1:numel(in)
if isprime(in(i))
sum = sum + in(i);
c=c+1;
end
end
out = sum/c;
Or better yet, no loop
out=mean(a(isprime(a)));

추가 답변 (3개)

the cyclist
the cyclist 2021년 3월 31일
After you correct the problem that @Geoff Hayes pointed out in his comment, you still have a mathematical one.
out = sum/2
is not the correct way to calculate the mean value from the sum of the prime values.
Is that enough of a hint?
Also, I'll just point out that you did not need to use the for loops here. You could have applied isprime to the whole input array at once, harnessing the vectorized nature of MATLAB.
  댓글 수: 1
Hidd_1
Hidd_1 2021년 3월 31일
편집: Hidd_1 2021년 3월 31일
Thanks I solved the problem! Here is my solution:
function out = meanOfPrimes(in)
[a,b]=size(in);
s = 0;
k=0;
for i = 1:a
for j = 1:b
if isprime(in(i,j))
s = s + in(i,j);
k = k+1;
end
end
end
out = s/k
end
[s] I wonder how can I solve it using only isprime? [/s]

댓글을 달려면 로그인하십시오.


Hernia Baby
Hernia Baby 2021년 3월 31일
As Geoff Hayes says, variable 'in' is NOT string type.
You can check the data type with class function.
in = [8 3 5 9];
class(in)
ans =
'double'
ーーーーーーーーーーーーーー
Therefore, it will work well when omitting str2num.
clear,clc;
in = [8 3 5 9];
[a,b]=size(in);
sum = 0;
for i = 1:a
for j = 1:b
if isprime(in(i,j))
in(i,j) = in(i,j);
sum = sum + in(i,j);
end
end
end
out = sum/2
out =
4

Abhinav
Abhinav 2023년 4월 26일
function out = meanOfPrimes(in)
primeTF = isprime(in);
primeTFIndex = find(primeTF);
x = [];
for i = 1:length(primeTFIndex)
x = [x, in(primeTFIndex(i))];
end
out = mean(x);
end

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by