finding data from unique id in two tables

5 ビュー (過去 30 日間)
azim
azim 2021 年 10 月 15 日
コメント済み: Rik 2021 年 10 月 15 日
hi,
i have a two tables one is the master table with the following fields:
table a
unique id name age
j1 john 34
j2 max 18
j3 pablo 17
j4 immy 32
and so on consisting of around 100 rows
and the second table is just a column of unique ids but not in the same order as the master table for eg:
table b
unique id
j3
j2
around 40 rows ( number can change)
how do i extract data from table a using unique id from table b and fill the name and age column in table b?
i want the output of table b to be
unique id name age
j3 pablo 17
j2 max 18
do i have use a for loop only and check each unique id or is there a better and faster way?
any help on this matter would be highly appreciated.
thanks

採用された回答

Rik
Rik 2021 年 10 月 15 日
You can use the ismember function.
  2 件のコメント
azim
azim 2021 年 10 月 15 日
thanks. just to confirm then to fill i use the for loop
Rik
Rik 2021 年 10 月 15 日
No need for any loops at all:
%Create the data
UID={'j1';'j2';'j3';'j4'};
name={'john';'max';'pablo';'immy'};
age=[34;18;17;32];
T1=table(UID,name,age);
UID={'j3';'j2'};
T2=table(UID);
%Find matches
[a,b]=ismember(T2.UID,T1.UID);
%Assign data
T2.name={T1.name{b}}.';
T2.age=T1.age(b);
T2
T2 = 2×3 table
UID name age ______ _________ ___ {'j3'} {'pablo'} 17 {'j2'} {'max' } 18

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by