averaging selected columns and saving to a new file

I have a data file with 1st column - time, 2nd column measurement and third one error. I want to average 1st column, 2nd column and 3rd column corresponding to same time over the whole file and make a new file with such values. Also by same time I mean same number before decimal e.g.
4500.34
4500.25
4500.68
3700.57
3700.45
3700.98
so on....
So in above case, 4500.34 4500.25 4500.68 will be considered with same time and 3700.57 3700.45 3700.98 with same time. Now I want to average 1st,2n,3rd columns of same time and make one new point and so on for all same time.
Please help

2 Comments

@Giru Mishra: please upload a sample file by clicking the paperclip button.

Sign in to comment.

 Accepted Answer

You can try this, here x is the matrix name containing 3 columns
[unique_time, ~, index_unique] = unique(floor(x(:,1)));
measurement_average = accumarray(index_unique, x(:,2), [], @mean);
error_average = accumarray(index_unique, x(:,3), [], @mean);
result = [unique_time measurement_average error_average];

10 Comments

Also since I am new to MATLAB, could you please explain the steps so that I can try to implement on my data file.
Run this code like following
x = load('data.txt'); %<-- load data from file
[unique_time, ~, index_unique] = unique(floor(x(:,1)));
measurement_average = accumarray(index_unique, x(:,2), [], @mean);
error_average = accumarray(index_unique, x(:,3), [], @mean);
result = [unique_time measurement_average error_average];
To understand try running each line one by one and see what is the output. Also, refer to the documentation of unique() and accumarray() to see what is the functionality of these MATLAB functions.
Thanks.. I will try once and get back to you... also I want outputs in decimal form.. its giving in exponential for.. what should I do? I tried format long but no change
Which statement is giving output in exponential notation and what is the output? I ran this code and all the values are in decimal notation.
I ran it over another file with time in more than 6 digits and its giving 'results' in exponential form. Also in the error if we squares of errors corresponding to same time then how to modify the code. please help
If you just want to display the result in decimal notation then you can try
vpa(result);
And to get the average of square error use
error_average = accumarray(index_unique, x(:,3), [], @(x) mean(x.^2));
it's giving results in exponential form
This is really basic matlab. To change the way matlab displays numbers, use the format function. For example,
format longg
Note that this only affects how matlab displays the numbers to you. The numbers are stored in memory the same (with full precision).
Dear Ameer... thanks for your reply. I will try that way.. Also, I want just squares of error points associated with measurements under same time and then divide by number of those points.
@Giru You are welcome. Yes, the line posted in the comment will take the average of square values under same time.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!