Average of two consecutive rows and importantly average of first and last row.
3 次查看(过去 30 天)
显示 更早的评论
Please find the picture attached for better understanding
I have a structure with multiple columns and rows. I want to create new structure which is an average of consecutive rows.
For example, in the image attached, I want to to new variable to data structure such that, to say date 20181201 with UID 1, then it is removed, while for UID 2, new variable is an average of PC_36km with UID 1 and UID 2, and for UID 3, new variable is an average of PC_36km with UID 2 and UID 3 so on and so forth...
Please let me know if you have any ideas...
2 个评论
the cyclist
2019-11-19
It would be much easier to help if you uploaded the structure (or a small representative sample) in a MAT file.
The image helps us understand, but the actual variable would allow us to test potential solutions.
回答(1 个)
Guillaume
2019-11-19
You have to be careful with the terminology you use. structures aren't tabular, they don't have columns and rows (well, they can but that's not the case for you). If it's possible to display a structure as tabular the variable browser will do so but that doesn't mean your structure works like a table.
What you have is a structure array, either 1xN (so just one row) or Nx1 (so just one column), displayed as rows, with 6 fields (displayed as columns). And it sounds like you want to add another field (not variable) to the structure.
I'm not entirely clear what it is you want to do, your explanation is a bit confusing. Whatever it is however, you'll be better off converting your structure into a table (which has rows and columns, and variables) and converting your Date field to a proper datetime. Easily done:
tdata = struct2table(yourstructure); %convert structure to table
tdata.Date = datetime(tdata.Date, 'ConvertFrom', 'yyyymmdd'); %convert date to datetime
Once that's done it's trivial to perform some aggregration according to some grouping rule. I've not really understand what you want to do but here is a example of calculating the average of each column for each data:
dailymean = groupsummary(tdata, 'Date', 'day', 'mean'); %calculate the mean for each day.
2 个评论
Guillaume
2019-11-19
Oh! ok:
PC = cat(3, yourstruct.PC_36km); %concatenate all PC matrices in 3D
mean_consPC = movmean(PC, 2, 3, 'Endpoints', 'discard');
newstruct = struct('PC_36km', num2cell(mean_consPC, [1 2]));
%not sure what else you want in your structure
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!