How do I calculate the mean of only positive values
27 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have a 3x24 array with negative and positive values, how do I calculate its mean value?
0 commentaires
Réponses (2)
Image Analyst
le 23 Déc 2020
Here's a well commented example:
m = 10 * rand(3, 24) - 5 % Create sample data (replace with your actual array).
posMap = m > 0 % Get map of where the positive values are.
m2 = m .* posMap; % Initializes m2.
m2(~posMap) = nan % Set negative values to nan so we can ignore them when we take the mean.
% Get mean of each column, meaning you average going down rows.
columnMeans = mean(m2, 1, 'omitnan')
% Get the mean of each row, meaning you average over all the columns in each row.
rowMeans = mean(m2, 2, 'omitnan')
% Get the mean of any positive value, regardless of which row or column it lives in.
overallMean = mean(m2(:), 'omitnan')
0 commentaires
dpb
le 23 Déc 2020
mean(X(X>0)) % global mean
or
arrayfun(@(i) mean(X(X(:,i)>0,i)),1:size(X,2)) % column means
depending upon which mean(s) one is after.
0 commentaires
Voir également
Catégories
En savoir plus sur Matrices and Arrays 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!