Matching columns of two different tables with different tablesizes
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Cris LaPierre
2021-8-11
I have the details from the deleted post. Here is my code for that question. Let me know if you have any questions.
% Create table1 and table2
WP1 = [0;0;145;169;1693;0;0;255;0;0;48;1382;0;0;55;156;0];
WP2 = [0;145;169;1693;1980;0;255;260;0;48;1382;1400;0;55;156;180;0];
duration = [0;25;10;260;35;0;67;5;0;25;379;17;0;43;89;22;0];
table1 = table(WP1,WP2,duration);
WP1 = [0;0;169;1693;0;0;255;0;0;48;0;0;0];
WP2 = [0;169;1693;1980;0;255;260;0;48;1400;0;180;0];
category = [0;0;1;0;0;0;0;0;0;1;0;0;0];
table2 = table(WP1,WP2,category);
% Add a variable to track 'runs'
table1.counts = cumsum(sum(table1.Variables,2)==0)
table2.counts = cumsum(sum(table2.Variables,2)==0)
% Create a table combining WP increments from both tables
WPs = sortrows(unique([table1.WP1 table1.WP2 table1.counts
table2.WP1 table2.WP2 table2.counts],'rows'),[3,1]);
% remove rows that are no longer needed
WPs(diff(WPs(:,1))>0 & diff(WPs(:,2))<=0,:)=[];
WPs = array2table(WPs,"VariableNames",["WP1","WP2","counts"])
% Add category and duration information
lkupCat = @(x,y,z) min(table2.category(table2.counts == z & table2.WP1<=x & table2.WP2>=y));
WPs.category = rowfun(lkupCat,WPs,'OutputFormat',"uniform");
lkupDur = @(x,y,z,c) c*min(table1.duration(table1.counts == z & table1.WP1<=x & table1.WP2>=y));
WPs.duration = rowfun(lkupDur,WPs,'OutputFormat',"uniform");
% Remove helper variable counts
WPs.counts = []
4 个评论
Cris LaPierre
2021-8-21
It is much easier to help if you share the actual data instead of screen shots. Save yout tables to a mat file and attach them to your post using the paperclip icon.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!