フィルターのクリア

Joining a column from one table to another by id

1 回表示 (過去 30 日間)
Adnan Habib
Adnan Habib 2020 年 4 月 13 日
コメント済み: Tommy 2020 年 4 月 13 日
I have two tables A and B.
A portion of Table A (1256X1) looks like this:
ID
'46181193'
'11933657'
'98101224'
'9810114303'
'781027845'
'63241627'
'89541678'
'228511401'
Where ID is cell
A portion of Table B (3079X2) looks like this:
ID N
'46181193' 3
'98101224' 1
'781027845' 1
'63241627' 6
'89541678' 1
'96292061' 1
'1496117981' 1
'11592285' 6
'32002285' 5
'36042285' 3
Here ID is cell and N is double. I want Table A to also have the column N by matching the ID variables from Table B. As can be seen clearly Table B has more rows, sometimes repeatation of the values of some older rows. But the end table that I want should have 1256X2 dimension

回答 (1 件)

Tommy
Tommy 2020 年 4 月 13 日
編集済み: Tommy 2020 年 4 月 13 日
If you know each ID in A is included in B exactly once, I believe this should work:
A = [A rowfun(@(rn) B.N(strcmp(B.ID,rn)), A, 'OutputVariableNames', 'N')];
(edit) Ok, try this:
A = [A rowfun(@(rn) B.N(find(strcmp(B.ID,rn),1)), A, 'OutputVariableNames', 'N')];
  5 件のコメント
Adnan Habib
Adnan Habib 2020 年 4 月 13 日
編集済み: Adnan Habib 2020 年 4 月 13 日
The edit code gives the following error message
"The function '@(rn)B.N(find(strcmp(B.ID,rn),1))' returned an output with more than one row when applied to the 2nd row in A."
What does this mean?
Tommy
Tommy 2020 年 4 月 13 日
Are you sure the 2nd ID in A is somwhere in B?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by