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
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.
tf=isprime(in);
count=nnz(tf);
tot=sum(sum(tf.*in));
out =tot/count;
why is this code not working
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
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?
very easy
weird flex, but ok
gj
cheating
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
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/14-find-the-numeric-mean-of-the-prime-numbers-in-a-matrix/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.
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!
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.
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.
Why did the 3rd test fail?It's working in Matlab.
*slow clap*
you sir, are a c programmer...
I tried using this method and it was success.. but.. :(
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.
This should work, but it fails the third test as it produces a value for ans that is 4.409e-16 out due to rounding errors.
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()?
C'mon, Cody Challenge writers! At least provide a correct example -- 2 is prime (except where prohibited by law.)
4767 Solvers
Reverse the Words (not letters) of a String
312 Solvers
What is the distance from point P(x,y) to the line Ax + By + C = 0?
291 Solvers
550 Solvers
882 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!