Average of two consecutive rows and importantly average of first and last row.

4 vues (au cours des 30 derniers jours)
nlm
nlm le 19 Nov 2019
Commenté : Guillaume le 19 Nov 2019
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 commentaires
the cyclist
the cyclist le 19 Nov 2019
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.
nlm
nlm le 19 Nov 2019
Please find it attached. Thank you

Connectez-vous pour commenter.

Réponses (1)

Guillaume
Guillaume le 19 Nov 2019
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 commentaires
nlm
nlm le 19 Nov 2019
Modifié(e) : Guillaume le 19 Nov 2019
I want to average every consecutive row. therefore my new structure will be N-1 size. Please find the data attached.
For example,
new(1).val = mean(PC_36km(2).PC_36km(1));
new(2).val = mean(PC_36km(3).PC_36km(2));
so on and so forth...
Guillaume
Guillaume le 19 Nov 2019
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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by