Mean calculations in a for-loop

조회 수: 5 (최근 30일)
xenon99942
xenon99942 2019년 1월 17일
편집: Andrei Bobrov 2019년 1월 23일
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?

답변 (2개)

xenon99942
xenon99942 2019년 1월 17일
편집: madhan ravi 2019년 1월 17일
Hello!
i think i have a first solution. Don't know if it's espensive or not:
mean_240= zeros(2976,1);
mean_240(1)=mean(T(1:216),'omitnan');
index = 2;
for i = 217:size(T,1)
if i<239
mean_240(index)=mean(T((i-(214+index)):i));
else
mean_240(index) = mean(T((i-238):(i+(index-2))),'omitnan');
%index = index+1;
end
index = index+1;
end
  댓글 수: 2
xenon99942
xenon99942 2019년 1월 17일
편집: xenon99942 2019년 1월 17일
sorry the index = index+1 should be outside the if statement. But no i am getting still a solution matrix with only zeros in it. can you see the error?
madhan ravi
madhan ravi 2019년 1월 17일
upload T

댓글을 달려면 로그인하십시오.


Andrei Bobrov
Andrei Bobrov 2019년 1월 17일
편집: Andrei Bobrov 2019년 1월 23일
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
Andrei Bobrov
Andrei Bobrov 2019년 1월 17일
편집: Andrei Bobrov 2019년 1월 17일
Thank you Steven for your clarification.
xenon99942
xenon99942 2019년 1월 21일
편집: xenon99942 2019년 1월 21일
no i don't want to discard the first 216 elements. I want to compute the "most-element"-mean, which is possible. thats why i want to increase the number of elements in each step till i reach datapoint index 240 from which i start to compute the backward 240-element-mean of every following element. I am using an old matlab version, so "movmean" is not available.
T is an 3192x1 vector of data type "double"
The above code doens't run anymore. Error message:
??? Error using ==> sum
Trailing string input must be 'double' or 'native'.
Error in ==> mean at 30
y = sum(x,dim)/size(x,dim);
Befor that error message ( the code is the same), i just got zeros as output. Can you see the error?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by