Problem 14. Find the numeric mean of the prime numbers in a matrix.
Solution Stats
Problem Comments

12 Comments
2 is prime, so the example solution should be (2+3+5)/3 = 3.33....
+1, the example is wrong :(
*blushing* Oops! At least we got the actual test suite right. Thanks for the notes. Fixed it.
i didn't understand the problem
good one...
I don't understand why this doesn't work for test 3: out=sum(in.*isprime(in))/sum(isprime(in))
it works for all other tests but test 3 requires an answer of 3 and the code gives an answer of 3.0000. Can anyone tell me why?
funny :)
good
Good one
The question topic is hard to understand.
Good Problem for beginners.
What is wrong with
a = find( isprime( in))
out = mean( in( a)) ?
It works perfectly in my own MATLAB environment
Solution Comments

2 Comments
the 3rd, I get result is 3.0000 on my computer, but it failed, i am very confused now.
prime = isprime(in);
out = sum(in.*prime)/sum(prime);
This happens because MATLAB works in floating point arithmetic, and not exact (decimal) arithmetic.
You can do a quick check in your computer on MATLAB  sprintf('%0.18f\n', out), after computing out and you'll see that the value is not exactly 3.

2 Comments
tf=isprime(in);
count=nnz(tf);
tot=sum(sum(tf.*in));
out =tot/count;
why is this code not working
good!

2 Comments
how can the mean of the primes of [ 1 2 3] be 2.5?
The mean of the primes which are 1 & 3 is 2.
1 is not a prime number, so therefore 2+3=5, 5/2=2.5

1 Comment
Why this is happening,
3
Fail
x = [3 3; 3 3];
y_correct = 3;
assert(isequal(meanOfPrimes(x),y_correct))
out = 3.0000
Assertion failed.
Why?

2 Comments
very easy
weird flex, but ok

1 Comment
gj

1 Comment
cheating

1 Comment
I also had problems with test 3, but with this code if've passed all tests:
function out = meanOfPrimes(in) mean = sum(in.*isprime(in))/sum(isprime(in)); out = round(mean,1); end

3 Comments
in= [3 3; 3 3]
in =
3 3
3 3
>> sum(in.*isprime(in))/sum(isprime(in))
ans =
3.0000
The fault is in two respects:
(1) the numerator and denominator for Test 3 will be vectors, not scalars, because that's how sum() works;
(2) the MATLAB algorithm for matrix division is therefore being employed, and it has introduced a truncation error. Your code yields a purported answer of 3 – (4.4409E–16) for Test 3. Of course, if only a few decimal places are displayed, this is shown as "3.0000".
To avoid this problem, reshape the input matrix to a vector. This can be done with the reshape command, but an easier way is to simply index as "in(:)".
This is done in https://au.mathworks.com/matlabcentral/cody/problems/14findthenumericmeanoftheprimenumbersinamatrix/solutions/1142672
Another effective (but somewhat less elegant) way of avoiding the problem is to nest your summations: sum(sum(sum( ... ))). (But you must use at least as many "sum" command as your matrices have dimensions.)
For debugging, try using the whos command.
@David Verrelli
Thanks! It works.

2 Comments
where is the problem here? It works on matlab.
the problem is that you cannot change the function name, try to use meanOfPrimes and it will work!

2 Comments
The code I wrote works on my MATLAB. Why is it that it shows that my solution is incorrect?
mean2 is a function from Image Processing Toolbox. Cody only supports MATLAB, not the toolboxes.

1 Comment
It's amazing that your solution size is just 10. I guest it contains just one line of code.
Do you mind sharing it?
Thank you.

1 Comment
Why did the 3rd test fail?It's working in Matlab.

1 Comment
*slow clap*
you sir, are a c programmer...

1 Comment
I tried using this method and it was success.. but.. :(

2 Comments
no reason this shouldn't work...
The problem is that when the sum function is applied to a matrix it produces a vector instead of a scalar.

1 Comment
This should work, but it fails the third test as it produces a value for ans that is 4.409e16 out due to rounding errors.

3 Comments
Why is this solution not correct? It works in case 3 also if i use it in Matlab. I don't understand this.
In case 3 the argument is a matrix, so the sum functions return vectors instead of scalars (which are what the problem wants). The / operator acting on vectors solves a least squares problem, whose answer happens to be 3 in this case (by chance), but the numerical calculation does not give exactly 3 (try subtracting 3 from the solution).
if you use sum() why won't you use simply mean()?

1 Comment
C'mon, Cody Challenge writers! At least provide a correct example  2 is prime (except where prohibited by law.)
Problem Recent Solvers7658
Suggested Problems

Find state names that start with the letter N
924 Solvers

Back to basics 20  singleton dimensions
256 Solvers

Get the elements of diagonal and antidiagonal for any mbyn matrix
364 Solvers

606 Solvers

Natural numbers in string form
745 Solvers
More from this Author95
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!