How to match date from 2 different matrices and plug in its corresponding times?

1 view (last 30 days)
I have two matrices containing dates and time in this format[year month date hour min]
diary_data=[2010 10 26 20 14; 2010 10 27 21 25; 2010 10 28 21 15; ...
2010 10 29 20 55; 2010 10 30 21 01];
diary_date=diary_data(:,1:3);
diary1_data=[2010 10 27 20 17; 2010 10 28 21 11; ...
2010 10 28 21 09; 2010 10 29 20 47];
diary1_date=diary1_data(:,1:3);
how can [I] match the diary_date with the diary1_date so that my output looks something like this:
matched_data=[2010 10 27 20 17 21 25; 2010 10 28 21 11 21 15; ...
2010 10 28 21 09 21 15; 2010 10 29 20 47 20 55];
so basically i want to display the diary1_data with the corresponding times from diary_data. The length of diary_data and diary1_data are not same and there are some repeated dates on diary1_data where I want the repeat the corresponding times from diary_data matrix.
my teacher told me that it would be better to use a for loop. for i=1:size(diary1_date,1) but I don't know how that is going to help me pick out the times from diary_data and output it as above.
  1 Comment
dpb
dpb on 21 Aug 2015
Start by converting the dates to date numbers; that'll leave you with a unique single integer value that can be compared identically. Then find the locations in the second array for each (hence the loop) with a logical comparison (many choices here, ismember is one possiblity) and then you've got the locations to use to build the final result by iterating over it.
Post your attempts and where you get a specific question; since you mention an instructor we'll treat it as homework so "show your work" rules... :)

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 21 Aug 2015
I do not understand how you selected that output, but consider using ismember() with the 'rows' option.
  1 Comment
dpb
dpb on 21 Aug 2015
Good idea on the 'rows' option in lieu of datenums, Walter. OP, note you'll want that over the first three columns to get the match on date, not time.
On the output it appears he's listed the hrs,min fields for the given day beginning with those find in diary1 before the corresponding times in diary

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!