Need to calculate the mean without considering the 0 counts. In [1 2 3 4 0 6 7], mean should be (1+2+3+4+6+7)/6 and then output place the mean of six number at place of 0.
1 回表示 (過去 30 日間)
古いコメントを表示
回答 (1 件)
dpb
2021 年 6 月 22 日
>> v=[1 2 3 4 0 6 7];
>> v(v==0)=mean(v(v~=0))
v =
1.0000 2.0000 3.0000 4.0000 3.8333 6.0000 7.0000
>>
3 件のコメント
Steven Lord
2021 年 6 月 23 日
Without any built-in functions? No. The + operator is implemented as the built-in function plus.
If you want to do it specifically without the built-in mean function, yes. One approach would be to loop through the vector, adding up the elements of the vector and keeping track as you add each element to the total of how many non-zero elements you've seen. At the end divide.
dpb
2021 年 6 月 23 日
Unless it's homework with such a condition, though, why would one not use logical addressing and mean(), though?
What's the point in having MATLAB if not going to use its features?
参考
カテゴリ
Help Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!