calculation of a mean matrix
54 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Subrat kumar sahoo
le 15 Août 2012
Commenté : Steven Lord
le 25 Avr 2023
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 commentaire
Yanbo
le 15 Août 2012
you might just simply add a to b, and them divide the sum by 2. Or, are you looking for a specific command?
Réponse acceptée
Oleg Komarov
le 15 Août 2012
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 commentaires
Plus de réponses (3)
Image Analyst
le 15 Août 2012
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 commentaires
Alfredo Scigliani
le 25 Avr 2023
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
le 25 Avr 2023
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.
Benjamin Klugah-Brown
le 9 Août 2020
what if matrix a and b have different size
5 commentaires
Walter Roberson
le 10 Août 2020
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);
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!