How can I select rows based on the value in a specific column and calculate the mean of those rows for all other columns to create a new table containing just the means?
2 views (last 30 days)
Show older comments
Elisabeth Slade
on 19 Jun 2018
Answered: Peter Perkins
on 3 Jul 2018
I have a table array 3152x25 containing data for variables that are monitored over time. In column 25 I have a vaiable 'elapsedHr' for the calculated elapsed time in hours from 0 to 72, with 40+ rows corresponding to each hourly time point. I would like to calculate the mean value of each column variable (columns 2 to 20) for each of the 73 time points and generate a new table containing this data.
2 Comments
Accepted Answer
Andrei Bobrov
on 19 Jun 2018
Edited: Andrei Bobrov
on 19 Jun 2018
TT = table2timetable(Port1a(:,2:end),'RowTime',datetime(Port1a{:,1}));
out = varfun(@mean,TT(:,[1:20,end]),'GroupingVariables','elapsedHr');
or just for your case
out = varfun(@mean,Port1a(:,[2:20,end]),'GroupingVariables','elapsedHr');
More Answers (1)
Peter Perkins
on 3 Jul 2018
You could also create a timetable using a duration time vector -- elapsed time from the start instead of absolute times. Equivalent to seconds(Port1a.elapsed, I think. If you did that, you could call retime with 'hourly' to get the hourly means.
Depends what eklse you are doing with the timestamps, of course.
0 Comments
See Also
Categories
Find more on Tables 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!