How to find common calendar of three matrices?

조회 수: 1 (최근 30일)
HN
HN 2019년 5월 1일
편집: dpb 2019년 5월 1일
I have three matrices that have common calendar. A part of these matrices is as follows:
A=[2000 1 2 230 240; 2000 1 3 240 250; 2000 1 5 260 270];
B=[2000 1 4 240 260; 2000 1 2 270 300; 2000 1 5 200 210];
C=[2000 1 5 240 260; 2000 1 7 270 300; 2000 1 2 200 210];
I want to extract the common calendar. For example, in the example above, the result is as follows:
D=[2000 1 2 230 240 2000 1 2 270 300 2000 1 2 200 210;
2000 1 5 260 270 2000 1 5 200 210 2000 1 5 240 260];
Thanks,

채택된 답변

dpb
dpb 2019년 5월 1일
편집: dpb 2019년 5월 1일
ABC=[A;B;C];
d=datetime(ABC(:,1:3));
g=findgroups(d);
D=splitapply(@(x) {vertcat(x)},ABC,g);
D=cell2mat(D(cellfun(@(c)size(c,1),D)>1));
D=reshape(D.',max(histc(g,unique(g)))*size(ABC,2),[]).'
The last presumes the number of matching dates is the same in all groups to be able to reshape to a rectangular array; the previous array will be the groups with more than one element in an column-oriented array; that step without cell2mat will leave a cell array with the duplicated dates in separate cells that could have differering numbers but still with at least two elements per cell.
  댓글 수: 5
dpb
dpb 2019년 5월 1일
I pointed out you've got issues in general with trying to reshape stuff...unless the data fit the very specific condition that there are the identical number of elements of each date, you're not going to have a rectangular array possible, in general.
What was the result of the previous step prior to trying to reshape? In particular, it would be most simple to look at the result also before the cell2mat so can see what the sizes of the various cell arrays are.
dpb
dpb 2019년 5월 1일
편집: dpb 2019년 5월 1일
Had a couple minutes, so...
D=splitapply(@(x) {vertcat(x)},ABC,g);
>> D(1:10)
ans =
10×1 cell array
{2×5 double}
{3×5 double}
{3×5 double}
{3×5 double}
{3×5 double}
{1×5 double}
{3×5 double}
{3×5 double}
{3×5 double}
{3×5 double}
>>
and you see the problem right off the bat -- there are two dates that match first, then three for several cases, then only one (that will get thrown out next step), but you can't reshape five elements into a 3*N X 5 array. Just can't be done.
Your expectations are simply unrealistic given the output format arrangement as you showed it with all the matching elements on a row when there aren't identically the same number of matches for every date.
>> D=cell2mat(D(cellfun(@(c)size(c,1),D)>1));
>> D(1:10,:)
ans =
2007 1 1 2 12
2007 1 1 1 1
2007 1 2 1 2
2007 1 2 3 12
2007 1 2 5 123
2007 1 3 1 2
2007 1 3 3 2
2007 1 3 5 123
2007 1 4 5 12
2007 1 4 3 123
>>
also illustrates the logic of grouping worked just fine; there are two entries for 1/1/2007 and three for 1/2/2007, both of which are > only one match.
As I suggested initially, for such a dataset, I'd guess it's highly likely the cell array form is the most useful but you'll have to decide just what it is you're really after.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by