How to take the average of each column of a matrix without including a certain number
Ältere Kommentare anzeigen
I have a matrix that has 864 rows and 7 columns and is filled with positive numbers and zeros. I want to take the average of each of those columns. However when MATLAB is taking the averages, I dont want it to include the zeros in its calculations. Can this be done?
Akzeptierte Antwort
Weitere Antworten (2)
Image Analyst
am 16 Jul. 2014
If you don't have nanmean because you don't have the Finance or Statistics toolbox, you can simply do
columnMeans = sum(a, 1) ./ sum(a~=0);
where "a" is the name of your matrix. This sums up the values in each column, then divides it by the sum of the non-zero values in each column.
1 Kommentar
Image Analyst
am 16 Jul. 2014
Even if you do have those toolboxes, I'd use this method instead.
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!