calculation of a mean matrix
이전 댓글 표시
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
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?
채택된 답변
추가 답변 (3개)
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
Oleg Komarov
2012년 8월 15일
This is the so much more intuitive version of my c2!
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
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.
a = [1 2 3; 2 3 4];
b = [2 3 4; 3 4 5];
c=[mean(a);mean(b)]
Benjamin Klugah-Brown
2020년 8월 9일
0 개 추천
what if matrix a and b have different size
댓글 수: 5
Walter Roberson
2020년 8월 9일
How would you like to define the results for locations that exist in one array but not in the other array?
Benjamin Klugah-Brown
2020년 8월 10일
Thinking of using zeros?
Walter Roberson
2020년 8월 10일
s=max(size(A),size(B)) ;
A1=A;
B1=B;
A1(end+1:s(1),1)=0;
B1(end+1,s(1),1)=0;
A1(1,end+1:s(2))=0;
B1(1,end+1:s(2))=0;
(A1+B1)/2
Benjamin Klugah-Brown
2020년 8월 10일
Thanks very much... by the ways does it work for NA too?
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);
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!