フィルターのクリア

Finding mean of matrix excluding 0

49 ビュー (過去 30 日間)
goyanks1989
goyanks1989 2016 年 5 月 13 日
コメント済み: Image Analyst 2016 年 5 月 13 日
I have a 4 1200x1200 matrixes which includes numerous 0 values, lets just call them 'a' 'b' 'c' and 'd'
How do i find the mean of each individual point in the matrix (that is a+b+c+d), while ignoring the 0 values?
Thank you for the help!

採用された回答

Star Strider
Star Strider 2016 年 5 月 13 日
If I understand correctly what you want to do, this will work:
a = randi([0 5], 10);
b = randi([0 5], 10);
c = randi([0 5], 10);
d = randi([0 5], 10);
abcd = cat(3, a, b, c, d); % Concatenate Along 3rd Dimension
abcd(abcd == 0) = NaN; % Set Zeros To ‘NaN’
mean_abcd = mean(abcd, 3, 'omitnan'); % Use 'omitnan' Argument And Take ‘mean’ Across 3rd Dimension

その他の回答 (1 件)

Image Analyst
Image Analyst 2016 年 5 月 13 日
You mean the mean at each location, like (a+b+c+d)/(# non-zeros)? How about
counts = (a~=0) + (b~=0) + (c~=0) + (d~=0);
theMeans = (a+b+c+d) ./ counts;
theMeans is a 1200-by-1200 matrix where each value is the mean of up to 4 values. If all 4 values are 0 then the value there is a nan.
  2 件のコメント
goyanks1989
goyanks1989 2016 年 5 月 13 日
Thank you for the answer. When I run my code I receive this error message
Error using ./
Integers can only be combined with integers of the same class, or scalar doubles
is this because my matrix is a 1200x1200 uint16? (and i am not exactly sure what uint16 means)
Image Analyst
Image Analyst 2016 年 5 月 13 日
They are not all uint16 - some must be different. Cast all to double and then do it.

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by