MATLAB: Average matrices based on unique value in structure resulting in a new structure

arrayfuncstructuressubfields vectorsunique values

I have a structure IMERG, with 19 fields. I have a field called "UID" based on which I want to average the fields of Lat and Lon which are 1*1700. For example, If UID is 1, then want to average all the Lat's with UID = 1 such that I get an 1 * 1700 for UID =1, and similarly for others.
I tried using arrayfucn, but it averages all the values within each IMERG(1).Lat, and also I'm unable to evaluate it based on unique values of UID. Please find the data attached of the structure I'm using.
A = arrayfun(@(x) mean(x.Lat),IMERG,'UniformOutput',1),
Any help is highly appreciated !! I have been trying this for very long now….

Best Answer

How about using splitapply function, like:
IMERG = struct2table(IMERG);
avgLat = splitapply(@(x) {mean([x{:}],2)}, IMERG.Lat, IMERG.TimeID+1);