Create new table variable filled with values from another table

조회 수: 3 (최근 30일)
Haris K.
Haris K. 2020년 10월 17일
댓글: Ameer Hamza 2020년 10월 17일
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).

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 10월 17일
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);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by