Create new table variable filled with values from another table

5 views (last 30 days)
Hi. I have the following tabels, T1 and T2:
Name1=["A","B","C","D"]'
Des1=["Shoubidou","Abra","Ca","Dabra"]'
T1=table(Name1, Des1)
Name2 =["C","B","B","A","B","B","A"]'
T2 = table(Name2)
I would like to create a new variable Des2 (in table T2), which for each T2 element contains the description taken from T1. The final result should be:
Des2=["Ca","Abra","Abra","Shoubidou","Abra","Abra","Shoubidou"]'
T2final = table(Name2, Des2)
One way to do this is to iterate over the elements of T2. Is there any other way without a for-loop?
Notice that some elements in T1 it is possible that they are not contained in T2 (just in case this changes your solution).

Accepted Answer

Ameer Hamza
Ameer Hamza on 17 Oct 2020
Try this
Name1=["A","B","C","D"]'
Des1=["Shoubidou","Abra","Ca","Dabra"]'
T1=table(Name1, Des1)
Name2 =["C","B","B","A","B","B","A"]'
T2 = table(Name2)
[~, idx] = ismember(Name2, Name1);
T2.Des2 = Des1(idx);

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!