How do I calculate a mean value of a vector and ignore from the "0" when appears inside the vectors?

18 ビュー (過去 30 日間)
Hello.
I need to calculate a mean value of a velocity vector. The vector contains a damaged cells which appears as "0" inside the vector. How do I calculate a mean value of a vector and ignore from the "0" when appears inside the vectors?

採用された回答

Star Strider
Star Strider 2016 年 9 月 24 日
If you have a recent release (I don’t remember when the 'omitnan' option appeared) or the Statistics and Machine Learning Toolbox nanmean function you can change the zeros to NaN and use those functions:
V = randi([0 9], 5)
V(V==0)= NaN
Out_1 = nanmean(V)
Out_2 = mean(V, 'omitnan')
V =
0 4 6 0 4
1 5 1 9 3
6 2 3 7 5
7 7 6 4 5
6 1 7 4 8
V =
NaN 4 6 NaN 4
1 5 1 9 3
6 2 3 7 5
7 7 6 4 5
6 1 7 4 8
Out_1 =
5 3.8 4.6 6 5
Out_2 =
5 3.8 4.6 6 5
The problem with the logical indexing approach is that it defaults to ‘linear indexing’ because the rows and columns are no longer equal. That produces a vector argument to the mean function, and the mean of the vector.
  4 件のコメント

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

その他の回答 (4 件)

George
George 2016 年 9 月 24 日
編集済み: George 2016 年 9 月 27 日
Replacing the 0s with NaN and using the 'omitnan' flag should do what you want.
>> A
A =
NaN NaN 11.6780 NaN NaN NaN NaN -23.3560 -35.0340 -42.8200
NaN NaN NaN NaN NaN NaN NaN -7.7850 -7.7850 -15.5710
NaN NaN NaN NaN NaN NaN 3.8930 NaN -3.8930 -15.5710
NaN NaN NaN NaN NaN NaN -3.8930 NaN NaN 15.5710
NaN NaN NaN NaN NaN NaN NaN -3.8930 -11.6780 -15.5710
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN -19.4630 -35.0340 -54.4980
NaN NaN NaN NaN NaN NaN NaN NaN -3.8930 -15.5710
NaN NaN NaN NaN NaN NaN NaN 3.8930 7.7850 11.6780
NaN NaN NaN NaN NaN NaN NaN -3.8930 -11.6780 -19.4630
NaN NaN NaN NaN NaN NaN NaN NaN -3.8930 NaN
NaN NaN NaN NaN NaN NaN NaN -3.8930 -3.8930 -11.6780
NaN NaN NaN NaN NaN NaN NaN -3.8930 -3.8930 -7.7850
NaN NaN NaN NaN NaN NaN NaN NaN -7.7850 -15.5710
NaN NaN NaN NaN NaN NaN NaN NaN NaN 7.7850
Trial>> meanCols = mean(A, 'omitnan')
meanCols =
NaN NaN 11.6780 NaN NaN NaN 0 -7.7854 -10.0562 -13.7742
>>
  2 件のコメント
Tzahi Shukrun
Tzahi Shukrun 2016 年 9 月 24 日
Hi! Thank you for your help. This command gives me the mean value of the entire matrix. I do i get the mean value of each vector in the matrix?

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


Image Analyst
Image Analyst 2016 年 9 月 24 日
編集済み: Image Analyst 2016 年 9 月 24 日
Try this
meanVelocity = mean(allVelocities(allVelocities ~= 0))
  3 件のコメント
Image Analyst
Image Analyst 2016 年 9 月 24 日
No it doesn't. It gives the means of only the non-zero elements of your vector. But now you've changed your question. Now you're saying that you have a matrix, NOT a vector. In that case, I'd use mean with the omitnan option after you've set 0's to nans, exactly what Star showed.
allVelocities(allVelocities == 0) = nan;
meanVelocity = mean(allVelocities, 'omitnan')
Note, in the above, allVelocities is a matrix (as in your comment), not a vector as you originally said. And meanVelocity is the column over rows (that is, going down columns) so you have one mean for every column.
Tzahi Shukrun
Tzahi Shukrun 2016 年 10 月 4 日
yeah... you are right, I didnt write my question well. Thanks to you, I have my answer!

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


Jan
Jan 2016 年 9 月 26 日
編集済み: Jan 2016 年 9 月 26 日
You do not have to replace the zeros by NaNs, because the zeros are neutral for the SUM already:
sum(A, 1) ./ sum(A ~= 0, 1)
  2 件のコメント
Thorsten
Thorsten 2016 年 9 月 27 日
For the sum, but not for the mean.
Jan
Jan 2016 年 9 月 28 日
@Thorsten: Exactly. Therefore you divide by the number of non-zeros and not by the number of elements. The posted code does exactly this.

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


Suraj Sudheer Menon
Suraj Sudheer Menon 2020 年 6 月 22 日
All non zero elements can be stored in another location using logical indexing and mean operation can be performed.
temp=A(A~=0); %stores the non zero values in temp.
ans=sum(temp)/nnz(A) ; %nnz returns number of non zero elements.
  1 件のコメント
Image Analyst
Image Analyst 2020 年 6 月 22 日
But since the sum of any number of zeros is still zero, the sum of A will be the sum of temp. So temp is not necessary, and you'd get the same thing from
ans=sum(A(:))/nnz(A) ;

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

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by