Copy If greater than column value
Show older comments
I have 3 tables (A, B, and C) and I want to copy the values from table A to table C if the value of column1 in table A is between the values of columns 2 and 3 in table B. Here's an example:
TableA
Col1 Col2 Col3
---- ---- ----
7 48 210
18 18 209
... (TableA has thousands of rows)
TableB
Col1 Col2 Col3
---- ---- ----
1 0 10
... (TableB has 10 rows)
So in this case, I want to copy the first row of TableA (7, 48, 210) to TableC because TableA.Col1 (7) is between the values of TableB.Col2 (0) and TableB.Col3 (10). Ultimately, I'll write an if/else statement so all rows of the data are copied to any of 10 tables according to an extension of this logic. I tried the following code:
if TableA.Col1(:,1) >= TableB.Col2(:,1) && TableA.Col1 < TableB.Col3(:,1)
TableC = TableA;
else
TableC = NaN;
end
When I try this, TableC only has 1 NaN in it so this is clearly not the right way to do this.
Accepted Answer
More Answers (0)
Categories
Find more on Calendar 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!