フィルターのクリア

Merging two matrices where they have the same data in 1st column

3 ビュー (過去 30 日間)
Cameron Spooner
Cameron Spooner 2016 年 8 月 13 日
回答済み: Star Strider 2016 年 8 月 13 日
I have 2 cell matrices that are set up as below;
A
01/01/16, 21, 32, 43
03/01/16, 22, 33, 44
05/01/16, 23, 34, 45
B
01/01/16, 0, 0, 0
02/01/16, 0, 0, 0
03/01/16, 0, 0, 0
04/01/16, 0, 0, 0
05/01/16, 0, 0, 0
And I would like to merge them such that if the two matrices have a date in common then the corresponding row from matrix A is copied to matrix B. Whilst dates not in common are left with 0 values to make an output C;
C
01/01/16, 21, 32, 43
02/01/16, 0, 0, 0
03/01/16, 22, 33, 44
04/01/16, 0, 0, 0
05/01/16, 23, 34, 45
I'm not very sure how to go about doing this so any help would be greatly appreciated.

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 13 日
編集済み: Azzi Abdelmalek 2016 年 8 月 13 日
A={'01/01/16', 21, 32, 43
'03/01/16', 22, 33, 44
'05/01/16', 23, 34, 45}
B={'01/01/16', 0, 0, 0
'02/01/16', 0, 0, 0
'03/01/16', 0, 0, 0
'04/01/16', 0, 0, 0
'05/01/16', 0, 0, 0}
C=B
idx1=ismember(B(:,1),A(:,1))
idx=ismember(A(:,1),B(:,1))
C(idx1,:)=A(idx,:)

その他の回答 (1 件)

Star Strider
Star Strider 2016 年 8 月 13 日
This works:
A = {'01/01/16', 21, 32, 43
'03/01/16', 22, 33, 44
'05/01/16', 23, 34, 45};
B = {'01/01/16', 0, 0, 0
'02/01/16,' 0, 0, 0
'03/01/16,' 0, 0, 0
'04/01/16', 0, 0, 0
'05/01/16', 0, 0, 0};
Adate = cellfun(@(x)datenum(x, 'mm/dd/yy'), A(:,1));
Bdate = cellfun(@(x)datenum(x, 'mm/dd/yy'), B(:,1));
Lidx = ismember(Bdate, Adate);
C = B;
C(Lidx,:) = A

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by