combining different size arrays based on time dimension and compute average for the last column

조회 수: 1 (최근 30일)
Hello:
I have two disimilar sized arrays, M1 (37988x5) and M2 (37376x5). The first four column shows year,month,day and hour and the last column show the value at each hour. I want to horizontally concatenate these two arrays based on time dimension. But since M2 has some days missing, which is in M1, I want to fill these values with Nan. So resulting matrix I need is 37988x7. The format of resulting array would be:
<Year> <Month> <Day> <hour> <last col of M1> <last col. of M2> <mean of last col. of M1 and M2>
I know I can create a time table array from both M1 and M2 but not sure how to horizontally concatenate two dismilar arrays together and compute average based on the values of M1 and M2. Since M2 doesn't contain some of the days, I want to fill 'nan' for those rows while computing the average.

채택된 답변

Fabio Freschi
Fabio Freschi 2019년 9월 8일
편집: Fabio Freschi 2019년 9월 8일
find rows in M2 that are present in M1 (assuming that all rows in M2 are present in M1
[~,iRows] = ismember(M2(:,1:4),M1(:,1:4),'rows');
Then preallocate the new M2 matrix
M2new = [M1(:,1:4) NaN(size(M1,1),1)];
Finally add the available M2 values
M2new(iRows,5) = M2(:,5);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by