Joining a table using a column of list

1 回表示 (過去 30 日間)
Douglas Chiang
Douglas Chiang 2020 年 8 月 28 日
コメント済み: Douglas Chiang 2020 年 9 月 3 日
Hi all, I have two tables as shown:
Table A (size mxn):
VarA VarB .....
------ -------
3 [1, 5, 7]
167 [2, 6, 9, 11]
......
Table B (size axb):
VarC VarD .....
------ -------
1 "X"
2 "B"
5 "E"
6 "F"
7 "G"
9 "I"
11 "R"
...
I would like to make use of Table B to join a new column to Table A like below:
New Table A:
VarA VarB VarE .....
------ ------- -------
3 [1, 5, 7] ["X", "E", "G"]
167 [2, 6, 9, 11] ["B", "F", "I", "R"]
......
Any help is much appreciated.
  3 件のコメント
Walter Roberson
Walter Roberson 2020 年 8 月 28 日
Does order matter? Alternately, will VarB always be in increasing order? Will VarC always be in increasing order?
Douglas Chiang
Douglas Chiang 2020 年 8 月 28 日
For VarB, you mean the order inside the array like [1,5,7]? Yes
For VarC, it is actually corresponds to the numbers in VarB and VarD shows their "Names", and the value increase down the column.

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

採用された回答

Mohammad Sami
Mohammad Sami 2020 年 8 月 28 日
編集済み: Mohammad Sami 2020 年 8 月 28 日
Try this
if true
tabA.VarE = cellfun(@(X)tabB.VarD(ismember(tabB.VarC,X)),tabA.VarB,'UniformOutput',false);
end
  6 件のコメント
Mohammad Sami
Mohammad Sami 2020 年 8 月 29 日
編集済み: Mohammad Sami 2020 年 8 月 29 日
You will need to convert the categorical array to cellstr, then convert it double.
tabA.VarB = cellstr(tabA.VarB);
tabA.VarB = regexprep(tabA.VarB,'[\[\]]','');
tabA.VarB = cellfun(@(x)str2double(strsplit(x,',')),tabA.VarB,'UniformOutput',false);
tabA.VarE = cellfun(@(X)tabB.VarD(ismember(tabB.VarC,X)),tabA.VarB,'UniformOutput',false);
Douglas Chiang
Douglas Chiang 2020 年 9 月 3 日
That do the job, thanks a lot.

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

その他の回答 (0 件)

カテゴリ

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