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

1 view (last 30 days)
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.

Accepted Answer

Fabio Freschi
Fabio Freschi on 8 Sep 2019
Edited: Fabio Freschi on 8 Sep 2019
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);

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!