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

3 Ansichten (letzte 30 Tage)
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 Kommentar
Geoff Hayes
Geoff Hayes am 31 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

David Hill
David Hill am 31 Mär. 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)));

Weitere Antworten (3)

the cyclist
the cyclist am 31 Mär. 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 Kommentar
Hidd_1
Hidd_1 am 31 Mär. 2021
Bearbeitet: Hidd_1 am 31 Mär. 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]

Melden Sie sich an, um zu kommentieren.


Hernia Baby
Hernia Baby am 31 Mär. 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 am 26 Apr. 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

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by