# Find average of multiples lines

38 views (last 30 days)
Vince Clementi on 1 Feb 2018
Commented: Marcus Leiwe on 27 Mar 2020
Hi All,
Having more trouble than I should finding the average line from 7 records. Each record is basically [T, (a-b)], where T is a common time interval (0:0.1:30) and (a-b) is the difference between two data sets (a-b, a-c, a-d, etc.).
I've designated each "difference" as such: diff, diff2, diff3, diff4, diff5, diff6, diff7. I am easily able to plot all of these records on the same plot. However, each method to find the mean of these lines fails. SO far, I have used mean([diff diff2, etc.],7). How do I go about finding the average for each common time point and then plot that?
Thanks.

Bob Nbob on 1 Feb 2018
No worries about any confusion, I'm just making sure I understand so I actually answer your question.
Ok, so what about adding a check to each column to confirm how many values exist?
for i = 1:301;
if isnan(array(:,i) ~= 0;
nansum = sum(isnan(array(:,i)));
average = sum(array(:,i))/(7-nansum);
else
average = sum(array(:,i))/7;
end
end
I can't guarantee it will work, but that should go through and check each column for NaN values. It will then add up the number of NaN values and reduce the average you are dividing by for each time slot.
I can't say that gives the actual average of the line, so it might be better to go through and fill in the NaN values with interpolations from their lines. This might help give a better average by having all seven values for each time step.
Vince Clementi on 1 Feb 2018
Thanks! I'll give it a shot.
Bob Nbob on 1 Feb 2018
Let me know if it works, and I can post as an official answer, or if you have some trouble shooting you can post your own answer.

Greg on 1 Feb 2018
Regarding the follow-up question found in the comments (NaN affecting the mean):
Use the 'omitnan' flag of the mean function (R2015a and newer):
%%%I wouldn't use "diff" as a variable name - it's a function
avg = mean([diff1, diff2, diff3, diff4, diff5, diff6, diff7],2,'omitnan');