How to calculate the mean of each row, excluding the first column?

4 vues (au cours des 30 derniers jours)
Julia
Julia le 12 Fév 2023
Commenté : Julia le 12 Fév 2023
For example, if I have this matrix
M=[0 3 4
0 6 5
0 9 2
0 3 1
0 8 9
0 4 6]
How can I calculate the mean of each row, excluding that 0? I tried
A=mean(M(:,2:end))
but this code gives me the mean of each column instead of each row.

Réponse acceptée

Arif Hoq
Arif Hoq le 12 Fév 2023
M=[0 3 4
0 6 5
0 9 2
0 3 1
0 8 9
0 4 6];
A=mean(M(:,2:end),2)
A = 6×1
3.5000 5.5000 5.5000 2.0000 8.5000 5.0000
  2 commentaires
Askic V
Askic V le 12 Fév 2023
It is also possible to use transpose:
A=mean(M(:,2:end).').'
Julia
Julia le 12 Fév 2023
Thank you!!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by