MATLAB: How to 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

MATLABselect rows

I have a table array 3152×25 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.

Best Answer

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');