Matching columns of two different tables with different tablesizes
Show older comments
I have two tables (attached):
In table1 I have two columns called WP_1 and WP_2.
In table 2 I have three columns called WP1, WP2 and WC.
For every pair of WP_1 and WP_2 I want to check if they are within any pair of WP1 and WP2.
If they are within a certain pair of WP1 and WP2, I want to take the value (0 or 1) of WC of that specific pair and print it to a new column in table1 --> table1.WC
If a pair of WP_1 and WP_2 does not fit any pair of WP1 and WP2 I want to print the value '2' in that specific line of table1.WC.
Here is an example how I want it to look like:

Explanation:
WP_1 and WP_2 in row 1 are [0,0] and fit within WP1 and WP2 in row1 [0,145] --> therefor WC in table 1 for that line is equal to WC in table2
WP_1 and WP_2 in row 2 are [0,5] and fit within WP1 and WP2 in row1 [0,145] --> therefor WC in table 1 for that line is equal to WC in table2
....
WP_1 and WP_2 in row 4 are [115,219] and DON'T fit within any pair of WP1 and WP2 in any row --> therefor WC in table 1 for that line is 2
WP_1 and WP_2 in row 5 are [219,262] and fit within WP1 and WP2 in row3 [169,1693] --> therefor WC in table 1 for that line is equal to WC in table2
My Code so far:
for n = 1:height(table1)
for m = 1:height(table2)
if table1.WP_1(n) >= table2.WP1(1:m) &...
table1.WP2_(n) <= table2.WP2(1:m)
table1.WC(n) = table2.WC(m);
else table1.WC(n) = 2;
end
end
end
I don't know how to check for every WP_1/WP_2 in table1 to check against all WP1/WP2 in table2..
Any help is very much appreciated - thank you!
6 Comments
KSSV
on 11 Aug 2021
Read about ismember.
Lukas Netzer
on 11 Aug 2021
Cris LaPierre
on 11 Aug 2021
I seem to recall a question yesterday very similar to this, though the third column in each table had different information (duration and category). Can't find it now.
Lukas Netzer
on 11 Aug 2021
Cris LaPierre
on 11 Aug 2021
It is. It's just I have a solution for what you posted yesterday.
Lukas Netzer
on 11 Aug 2021
Edited: Lukas Netzer
on 11 Aug 2021
Accepted Answer
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!