フィルターのクリア

Table Assignment using two tables of different height.

10 ビュー (過去 30 日間)
kuku hello
kuku hello 2022 年 2 月 9 日
コメント済み: kuku hello 2022 年 2 月 10 日
I have two tables of different heights: table1 and table2.
Both tables have the field x and table2 also has the field y.
I want to assign to table1 the parameter y where the table1 x parameter is equal to the table2 x parameter.
Since the tables are big It would be too inefficient of me to try and do double for loops I believe. Can someone help me with an efficient code to do that?
To be more informative, I'd like to do:
table1(table1.x == table2.x).y = table2.y
But since the two tables are not in the same height I can't do that.

採用された回答

Stephen23
Stephen23 2022 年 2 月 10 日
You can use ISMEMBER like this:
T1 = array2table([8,5;9,9;2,9;9,2;6,9;1,9;3,5])
T1 = 7×2 table
Var1 Var2 ____ ____ 8 5 9 9 2 9 9 2 6 9 1 9 3 5
T2 = array2table([6,0;7,0.1;8,0.2;9,0.3])
T2 = 4×2 table
Var1 Var2 ____ ____ 6 0 7 0.1 8 0.2 9 0.3
[X,Y] = ismember(T1.Var1,T2.Var1);
T1.Var2(X) = T2.Var2(Y(X))
T1 = 7×2 table
Var1 Var2 ____ ____ 8 0.2 9 0.3 2 9 9 0.3 6 0 1 9 3 5
  1 件のコメント
kuku hello
kuku hello 2022 年 2 月 10 日
thank you very much! It works perfectly!

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

その他の回答 (1 件)

Adam Danz
Adam Danz 2022 年 2 月 9 日
There are several ways to join tables.
If you get stuck, provide an example of the two tables and the expected result after joining the tables, and show what you've tried so we can help out.
  1 件のコメント
kuku hello
kuku hello 2022 年 2 月 10 日
編集済み: kuku hello 2022 年 2 月 10 日
Thanks I wasn't aware of those functions, and yet the variable x is not equal in both tables.
Meaning some x variables in table 1 exist and in table 2 they don't and vice versa. I see that join ain't what I'm looking for and the innerjoin deletes the rows that are not in common. outerjoin would duplicate rows with the same x since in table2 x values repeat themselves.

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

カテゴリ

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

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by