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

3 ビュー (過去 30 日間)
Hidd_1
Hidd_1 2021 年 3 月 31 日
回答済み: Abhinav 2023 年 4 月 26 日
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 件のコメント
Geoff Hayes
Geoff Hayes 2021 年 3 月 31 日
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.

サインインしてコメントする。

採用された回答

David Hill
David Hill 2021 年 3 月 31 日
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)));

その他の回答 (3 件)

the cyclist
the cyclist 2021 年 3 月 31 日
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 件のコメント
Hidd_1
Hidd_1 2021 年 3 月 31 日
編集済み: Hidd_1 2021 年 3 月 31 日
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]

サインインしてコメントする。


Hernia Baby
Hernia Baby 2021 年 3 月 31 日
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 2023 年 4 月 26 日
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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by