Calculate mean of a matrix columnwise if a condition is met

8 vues (au cours des 30 derniers jours)
susman
susman le 12 Août 2020
Commenté : hosein Javan le 12 Août 2020
I am stuck on a small coding issue.
I have a big matrix "A" and a scalar value "b" and I want to determine,
Expected value = Mean ( A -b) if A < b
for example if,
A = [10 10 10;10 5 5;10 5 5]
b = 10
C = b - A
C = [0 0 0; 0 -5 -5; 0 -5 -5]
Calculate mean = Expectation [C / (A<b ]
calculations of mean should be columnwise.
  2 commentaires
Cris LaPierre
Cris LaPierre le 12 Août 2020
Could you complete your example? What is the resulting value(s) of C for the example you shared?
susman
susman le 12 Août 2020
done!

Connectez-vous pour commenter.

Réponse acceptée

hosein Javan
hosein Javan le 12 Août 2020
A = [10 10 10;10 5 5;10 5 5]
b = 10
C = b - A
C(A>=b) = nan % replace all data out of specified range by nan
mean(C,'omitnan') % mean columns omitting nan
  2 commentaires
susman
susman le 12 Août 2020
Thats absolutely perfect! Thanks
hosein Javan
hosein Javan le 12 Août 2020
you're welcome. don't mention it.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur NaNs 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!

Translated by