There will always be at least one prime in the matrix.
Input in = [ 8 3
5 9 ]
Output out is 4 or (3+5)/2
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?
Why this is happening,
x = [3 3; 3 3];
y_correct = 3;
out = 3.0000
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.
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.)
Back to basics 13 - Input variables
sum of first 'n' terms
Evaluating a polynomial
Make a checkerboard matrix
Determine if input is odd
Reverse Run-Length Encoder
It dseon't mettar waht oedrr the lrettes in a wrod are.
Return the largest number that is adjacent to a zero
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