taking average and saving to new file
조회 수: 1 (최근 30일)
이전 댓글 표시
hey everybody... i am new to MATLAB.. i was facing problem with a small code.
i have a data file with about 100 points in x and y. i want to make a file with average of points in a bin of 3 i.e new x1 will be average of old(x1,x2,x3) and similarly for y...
Please guys help me out with it...
thanks :)
댓글 수: 0
답변 (1개)
Michael Haderlein
2014년 8월 22일
So, what should the new x2 be? The average of old (x2, x3, x4) or the average of old (x4, x5, x6)?
In the first case, it's basically smoothing the data. So you can use the smooth() function with a span of 3. Eventually, you want to remove the first and the last value after smoothing as they are the first and the last value of the original vectors (no mean here).
In the second case, you can use the following function if the length of x is multiple of 3:
xavg=mean(reshape(x,length(x)/3,3));
if the length is e.g. 100, I guess you only want to use the first 99 values. Then you can use
xavg=mean(reshape(x(1:3*fix(length(x)/3)),fix(length(x)/3),3));
댓글 수: 3
Michael Haderlein
2014년 8월 22일
Uhm, that's something entirely different?
binsize=10;
ind=1+fix(x/binsize);
x2=zeros(1,max(ind));
y2=zeros(1,max(ind));
for cnt=1:max(ind)
x2(cnt)=mean(x(ind==cnt));
y2(cnt)=mean(y(ind==cnt));
end
Don't know if there's a more elegant way, but this one works (at least in case that now is what you want).
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!