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
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?
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.
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,
x = [3 3; 3 3];
y_correct = 3;
out = 3.0000
weird flex, but ok
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]
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.
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?
Why did the 3rd test fail?It's working in Matlab.
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.)
Sum all integers from 1 to 2^n
"Low : High - Low : High - Turn around " -- Create a subindices vector
kmph to mps
Remove the Zero
It dseon't mettar waht oedrr the lrettes in a wrod are.
Extract leading non-zero digit
Remove the vowels
Find the treasures in MATLAB Central and discover how the community can help you!
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Contact your local office