フィルターのクリア

calculation of a mean matrix

39 ビュー (過去 30 日間)
Subrat kumar sahoo
Subrat kumar sahoo 2012 年 8 月 15 日
コメント済み: Steven Lord 2023 年 4 月 25 日
Hi I have two matrices
a = [1 2 3; 2 3 4]
and
b = [2 3 4; 3 4 5];
I want a mean output matrix "c," whose output should be
c= [1.5 2.5 3.5; 2.5 3.5 4.5].
so basically "c" should have a mean of respective parameters and same dimension as "a" and "b". Can someone help?
Thanks, Subrat
  1 件のコメント
Yanbo
Yanbo 2012 年 8 月 15 日
you might just simply add a to b, and them divide the sum by 2. Or, are you looking for a specific command?

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

採用された回答

Oleg Komarov
Oleg Komarov 2012 年 8 月 15 日
Unfortunately your example doesn't allow to propose a unique solution, i.e.:
c1 = [mean(a); mean(b)]
c2 = squeeze(mean(cat(3,a,b),3));
c1 simply takes the vertical mean (along rows) of a and then concatenates the vertical mean of b
c2 takes the mean of row 1 from a AND b and then concatenates the mean of the second row fro the two matrices.
Which one do you want?
  2 件のコメント
Subrat kumar sahoo
Subrat kumar sahoo 2012 年 8 月 15 日
Thanks Oleg, I wanted the operation like c2 is what I was looking for. Thanks again. Subrat
Subrat kumar sahoo
Subrat kumar sahoo 2012 年 8 月 15 日
I have a bit different requirement now: if "a" and "b" happens to be two elements of the same cell like d{1} and d{2} then is there a possibility of getting "c2" (*c2 = squeeze(mean(cat(3,a,b),3));*) with elements "a" and "b" (i.e. now d{1} and d{2}) picked thru a "for" loop? Thanks, Subrat

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

その他の回答 (3 件)

Image Analyst
Image Analyst 2012 年 8 月 15 日
a = [1 2 3; 2 3 4];
b = [2 3 4; 3 4 5];
c = (a+b)/2
In the command window:
c =
1.5 2.5 3.5
2.5 3.5 4.5
  3 件のコメント
Alfredo Scigliani
Alfredo Scigliani 2023 年 4 月 25 日
what if you have a ridculous amount of matrices (1000) and you want to find the average? I think a for loop, but not sure how.
Steven Lord
Steven Lord 2023 年 4 月 25 日
what if you have a ridculous amount of matrices (1000)
Then I'd recommend you revise the code to avoid that scenario. More likely than not you dynamically created variables with numbered names like x1, x2, x3, etc.
Can you do that? Yes.
Should you do this? The general consensus is no. That Answers post explains why this is generally discouraged and offers several alternative approaches.

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


Thomas
Thomas 2012 年 8 月 15 日
編集済み: Thomas 2012 年 8 月 15 日
a = [1 2 3; 2 3 4];
b = [2 3 4; 3 4 5];
c=[mean(a);mean(b)]

Benjamin Klugah-Brown
Benjamin Klugah-Brown 2020 年 8 月 9 日
what if matrix a and b have different size
  5 件のコメント
Benjamin Klugah-Brown
Benjamin Klugah-Brown 2020 年 8 月 10 日
Thanks very much... by the ways does it work for NA too?
Walter Roberson
Walter Roberson 2020 年 8 月 10 日
If by NA you mean NaN, then you would have to use
mean(cat(3, A1, B1), 3, 'omitnan')
or you would have to use something like
maskA = isnan(A1);
maskB = isnan(B1);
C1 = (A1 + B1) / 2;
C1(maskA) = B1(maskA);
C1(maskB) = A1(maskB);

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

カテゴリ

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