compute means between files

3 views (last 30 days)
anna
anna on 5 Feb 2014
Commented: Walter Roberson on 10 Feb 2014
Hi,
I'm trying to create a series of vectors that stores the means from vectors located in a series of files. For example if file 1 (excel file) contains vector (1,2,3) and vector (4,5,6) while file 2 contains vector (7,8,9) and (10,11,12). I want to get vector 1 =(mean(1,7),mean(2,8),mean(3,9)) and vector 2=(mean(4,10),mean(5,11),mean(6,12)).
Any ideas on the shortest way to do this?

Answers (1)

Walter Roberson
Walter Roberson on 5 Feb 2014
If the vectors are stored as columns in the files, each column a different vector, then
d1 = xlsread('FirstFile.xls');
d2 = xlsread('SecondFile.xls');
means = (d1 + d2)/2;
will then be vectors as a series of columns, each column the mean of the corresponding file columns.
If you have a number of files, say N of them, whose names are in "filenames" then
data = cell(N,1);
for J = 1 : N
data{J} = xlsread(filenames{J});
end
means = mean(cat(3, data{:}),3);
  2 Comments
anna
anna on 6 Feb 2014
Thanks, I had originally tried that version (the 2nd one) and on selecting the column arrays from the original files I used something like this:
data = cat(3,data{:}); Mean1 = mean(data(:,2,:),3);
for example, to select column 2 from each of the files and calculate the means of those. These 2 lines would replace as in your comment:
means = mean(cat(3, data{:}),3);
But I am not getting the correct Mean1. It is a column vector but doesn't correspond to the right values. Am I not selecting them correctly?
Walter Roberson
Walter Roberson on 10 Feb 2014
My test data appears to get the right answer. Can you produce a small bit of sample data that it gets wrong?

Sign in to comment.

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!