필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Pree
Pree 2015년 8월 21일
댓글: dpb 2015년 8월 21일
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
dpb
dpb 2015년 8월 21일
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... :)

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2015년 8월 21일
I do not understand how you selected that output, but consider using ismember() with the 'rows' option.
  댓글 수: 1
dpb
dpb 2015년 8월 21일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by