Averaging sections of a vector
Mostra commenti meno recenti
A=[1 3 -999 -999 -999 -999 4 9 8 -999 -999 -999]; Here I want to get averages of three elements by ignoring -999. But in the final answer, if there were cases with three -999 in a given sector, it needs to be replaced by NaN. For an example, final answer of the above should B=[2 NaN 7 NaN]
Thanks.
Risposta accettata
Più risposte (1)
Andrei Bobrov
il 28 Lug 2017
z = A;
z(z == -999) = nan;
t = ~isnan(z);
B = accumarray(cumsum(diff([~t(1);t(:)]) ~= 0),z,[],@mean);
Categorie
Scopri di più su NaNs in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!