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

3 visualizaciones (últimos 30 días)
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 comentario
Geoff Hayes
Geoff Hayes el 31 de Mzo. de 2021
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.

Iniciar sesión para comentar.

Respuesta aceptada

David Hill
David Hill el 31 de Mzo. de 2021
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)));

Más respuestas (3)

the cyclist
the cyclist el 31 de Mzo. de 2021
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 comentario
Hidd_1
Hidd_1 el 31 de Mzo. de 2021
Editada: Hidd_1 el 31 de Mzo. de 2021
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]

Iniciar sesión para comentar.


Hernia Baby
Hernia Baby el 31 de Mzo. de 2021
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 el 26 de Abr. de 2023
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

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by