필터 지우기
필터 지우기

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

조회 수: 3 (최근 30일)
nlm
nlm 2019년 11월 19일
댓글: Guillaume 2019년 11월 19일
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
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
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
nlm
nlm 2019년 11월 19일
편집: Guillaume 2019년 11월 19일
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 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 CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by