taking average and saving to new file

2 views (last 30 days)
aditi
aditi on 22 Aug 2014
Commented: Michael Haderlein on 22 Aug 2014
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 :)

Answers (1)

Michael Haderlein
Michael Haderlein on 22 Aug 2014
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 Comments
aditi
aditi on 22 Aug 2014
i will explain with example...
if my data file looks like:
4 20
2 24
6 33
11 22
17 222
29 344
now for limits of x 1 to 100 and for binning of value 10 i.e for intervals of 10 i will have 1 point for interval 1 to 10 which will be average of 4,2,6 in above example (as only these values lie in 1st interval of value 10) and corresponding y value will b average of y values associated with them. similarly 2nd value of x will be average of 11,17 as only these lie in next interval of 10 i.e 11-20... and so on... i hope i made my point clear
Michael Haderlein
Michael Haderlein on 22 Aug 2014
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).

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!