필터 지우기
필터 지우기

find matching character string across tables; take value from row with matching string in one table and add to new column in the other table

조회 수: 4 (최근 30일)
I have two tables. I want to take the i-th entry from table 1, find the matching string in table 2, and then grab a value from a different column in table 2 and add it to table 1. For example:
Table 1: Table 2:
Var1 Var2 Var 1 Var2
1 a 50 c
2 b 40 d
3 c 25 a
I want to compare Table1(1,2) to Table2(:,2), find that the 3rd row in Table 2 has the entry I want, and then add the value in Column 1 (25) to a new column in Table 1:
Table 1_updated:
Var 1 Var2 Var3
1 a 25
.
.
.
I have been trying something like this:
Table2(strcmp(Table1(1,3), Table2(:,3)==1,);
But this gives me an empty 0 x 3 table
Any ideas? Thanks in advance!

답변 (1개)

Alexis Black
Alexis Black 2019년 9월 28일
I solved it - I needed to use Table.Var2 instead of indexing the table:
Table2(strcmp(Table1.Var3(1), Table2.Var3(:)==1,);
  댓글 수: 1
Mia Grahn
Mia Grahn 2021년 7월 7일
How did you actually make the table you wanted to? I am trying to do a for loop right now that looks for the same as you have put for table 1_updated cannot seem to figure out how to append to each row

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by