Getting NaN for the mean of a vector, is this normal?

2 ビュー (過去 30 日間)
Loic Roy-Seger
Loic Roy-Seger 2020 年 10 月 9 日
編集済み: Stephen23 2020 年 10 月 9 日
Hi there,
So I wrote a code here an when I try to calc the mean I get a NaN result for the final mean. This is due inaround 5 hours and I have no idea what to do. Any ideas?
%% Get user input
Prototype_id = input('Please Enter the prototype ids in vector form: ');
Time_intial = input('Please input the activation times in vector form: ');
Time_Final = input('Please inputthe time of decay in vector form: ');
% Calculate the mean time for activation
Mean = mean(Time_intial);
% Calculate the mean time for decay
Mean_f = mean(Time_Final);
%% Display the result
disp(['The number of prototypes is: ', num2str(Prototype_id)]);
disp(['The average amount of time for activation is: ', num2str(Mean)]);
disp(['The average amount of time for decay is: ', num2str(Mean_f)]);
  3 件のコメント
Loic Roy-Seger
Loic Roy-Seger 2020 年 10 月 9 日
Please Enter the prototype ids in vector form: [3:8:14:17:21:22:25:32:34:40:46:43:48:49]
Please input the activation times in vector form: 0:12:6:17:32:14:35:22:10:18:29:23:4:15
Please inputthe time of decay in vector form: 130:115:158:180:250:292:117:217:231:172:123:182:218:200
Loic Roy-Seger
Loic Roy-Seger 2020 年 10 月 9 日
At least, that's what I plugged in

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

回答 (2 件)

Ameer Hamza
Ameer Hamza 2020 年 10 月 9 日
編集済み: Ameer Hamza 2020 年 10 月 9 日
I think you misunderstand the colon operator. Read about it here: https://www.mathworks.com/help/matlab/ref/colon.html. You should input these values like this
prototype ids
[3, 8, 14, 17, 21, 22, 25, 32, 34, 40, 46, 43, 48, 49]
activation times
[0, 12, 6, 17, 32, 14, 35, 22, 10, 18, 29, 23, 4, 15]
time of decay
[130, 115, 158, 180, 250, 292, 117, 217, 231, 172, 123, 182, 218, 200]
  2 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 10 月 9 日
Yes, it might cause confusion and bug in some cases. I have updated the answer to use commas as the separator.

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


Sudhakar Shinde
Sudhakar Shinde 2020 年 10 月 9 日
NaN returns the scalar representation of "not a number". Operations return NaN when they have undefined numeric results, such as 0/0 or 0*Inf.
  2 件のコメント
Loic Roy-Seger
Loic Roy-Seger 2020 年 10 月 9 日
See I know that and that's the reason why I'm confused because it's a vector with normal numbers that should work
Sudhakar Shinde
Sudhakar Shinde 2020 年 10 月 9 日
編集済み: Sudhakar Shinde 2020 年 10 月 9 日
time of decay in vector form gives an empty vector and hence mean is NaN.

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

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by