Is it possible to vectorize this ?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
A matrix A(100x1000) a selective column defined in B(1x1000). The required processing is the mean of non zero elements of each column of A referenced by B. Without the use of loops.
Regards
0 commentaires
Réponse acceptée
Matt J
le 26 Avr 2021
Modifié(e) : Matt J
le 26 Avr 2021
A(~A)=nan;
result=mean(A(:,B),1,'omitnan')
Plus de réponses (1)
Jan
le 26 Avr 2021
AB = A(:, B);
result = sum(AB, 1) ./ sum(AB ~= 0, 1);
mean('omitnan') replaces the NaNs by zeros for the summation and calculates the number of non-NaNs by sum(~isnan(AB)). Therefore this code should be faster, because it avoids replacing zeros by NaNs and back to zeros again.
1 commentaire
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!