Create new table variable filled with values from another table

6 vues (au cours des 30 derniers jours)
Haris K.
Haris K. le 17 Oct 2020
Commenté : Ameer Hamza le 17 Oct 2020
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).

Réponse acceptée

Ameer Hamza
Ameer Hamza le 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);
  2 commentaires
Haris K.
Haris K. le 17 Oct 2020
You're da man. Thx!
Ameer Hamza
Ameer Hamza le 17 Oct 2020
I am glad to be of help!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Tags

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by