Creating a table by extracting rows from a table based off their values in relation to another table of a different size.
3 views (last 30 days)
Show older comments
I have two tables of different size, for example, I have a 16x4 table with headers a,b,c and d and I have another 6x2 table with headers c and d. I have constructed both tables in matlab
a = [11;33;55;77;99;23;45;67;89;87;65;43;10;1;3;5];
b = [22;44;66;88;12;34;56;78;98;76;54;32;21;2;4;6];
c = [2;2;2;2;4;4;4;4;6;6;6;6;8;8;8;8];
d = [2;4;6;8;2;4;6;8;2;4;6;8;2;4;6;8];
T1 = table(a,b,c,d)
cc = [1.2;1.6;5.5;6.7;5.9;2.3];
dd = [2.5;4.6;3.5;3.2;1.5;1.9];
T2 = table(cc,dd)
T3 = T1(abs(T1.c-T2.cc)<=1 & abs(T1.d-T2.dd)<=1)
I wish to construct another table by filtering out rows based on values of abs(c - cc) <= 1 and abs(d - dd)<= 1 as the conditions and construct a new table using the extracted rows. looking like this in due to the order of the previous table t2:
.
But I couldn't do it as the number of variables are different. Any help will be appreciated!
0 Comments
Answers (1)
Stephen23
on 15 Sep 2022
a = [11;33;55;77;99;23;45;67;89;87;65;43;10;1;3;5];
b = [22;44;66;88;12;34;56;78;98;76;54;32;21;2;4;6];
c = [2;2;2;2;4;4;4;4;6;6;6;6;8;8;8;8];
d = [2;4;6;8;2;4;6;8;2;4;6;8;2;4;6;8];
T1 = table(a,b,c,d)
cc = [1.2;1.6;5.5;6.7;5.9;2.3];
dd = [2.5;4.6;3.5;3.2;1.5;1.9];
T2 = table(cc,dd)
ixc = any((T1.c-reshape(T2.cc,1,1,[]))<=1,3);
ixd = any((T1.d-reshape(T2.dd,1,1,[]))<=1,3);
T3 = T1(ixc&ixd,:)
See Also
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!