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

3 vues (au cours des 30 derniers jours)
Hidd_1
Hidd_1 le 31 Mar 2021
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 commentaire
Geoff Hayes
Geoff Hayes le 31 Mar 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.

Connectez-vous pour commenter.

Réponse acceptée

David Hill
David Hill le 31 Mar 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)));

Plus de réponses (3)

the cyclist
the cyclist le 31 Mar 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 commentaire
Hidd_1
Hidd_1 le 31 Mar 2021
Modifié(e) : Hidd_1 le 31 Mar 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]

Connectez-vous pour commenter.


Hernia Baby
Hernia Baby le 31 Mar 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 le 26 Avr 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

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by