Mean calculations in a for-loop
Afficher commentaires plus anciens
mean_240= zeros(2976,1); %preallocate vector
mean_240(1)=mean(T(1:216),'omitnan');
index = 2;
for i = 217:size(T,1)
mean_240(index) = mean(T((i-239):i),'omitnan');
index = index+1;
end
Hi all!
i have a 3192x1 vector and i want to calculate the 'backward' mean of every element of T ,considering the last 240 elements of each datapoint. i want to start at datapoint T(217) (the first 216 elements mean are calctulaeted seperately outside the lop), but as you can see, there are not enough values from 1:216 to calculate a 240element-mean and the above code won't work. so i want to modify the mean calculation until there are enough elements to proceed with the 240-mean-loop.
E.G. for the first for-loop step i = 217 the mean would be
mean_240(2) = mean(T((i-216):i),'omitnan');
and for the next step i = 218 we can use one more sample point to calculate the mean
mean_240(3) = mean(T((i-217):i),'omitnan');
.. and so on until we reach i = 240, then we above loop would work and we can calculate the whole 240point mean for the other sample points. how can i write that?
Réponses (2)
xenon99942
le 17 Jan 2019
Modifié(e) : madhan ravi
le 17 Jan 2019
2 commentaires
xenon99942
le 17 Jan 2019
Modifié(e) : xenon99942
le 17 Jan 2019
madhan ravi
le 17 Jan 2019
upload T
Andrei Bobrov
le 17 Jan 2019
Modifié(e) : Andrei Bobrov
le 23 Jan 2019
0 votes
EDIT
Let A is your vector with length n = 3192.
i1 = 217;
ii = 240;
B = A;
n = numel(A);
lo = isnan(A);
B(lo) = 0;
p = ones(n,1);
p(lo) = 0;
k = ones(ii,1);
Bp = conv2([B,p],k);
out = Bp(i1:n,1)./Bp(i1:n,2);
3 commentaires
Steven Lord
le 17 Jan 2019
You can specify the 'Endpoints', 'discard' name-value pair arguments rather than post-processing to throw away the first 216 elements.
Though I am a bit confused about the disconnect in the original message between "i want to start at datapoint T(217)" and "there are not enough values from 1:216 to calculate a 240element-mean" since there aren't enough values from 1:217 to compute a 240-element mean either. So maybe 'discard' is not what the original poster wants.
Andrei Bobrov
le 17 Jan 2019
Modifié(e) : Andrei Bobrov
le 17 Jan 2019
Thank you Steven for your clarification.
xenon99942
le 21 Jan 2019
Modifié(e) : xenon99942
le 21 Jan 2019
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!