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

1 Ansicht (letzte 30 Tage)
Pree
Pree am 21 Aug. 2015
Kommentiert: dpb am 21 Aug. 2015
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 Kommentar
dpb
dpb am 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... :)

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 21 Aug. 2015
I do not understand how you selected that output, but consider using ismember() with the 'rows' option.
  1 Kommentar
dpb
dpb am 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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by