# Creating a table by extracting rows from a table based off their values in relation to another table of a different size.

조회 수: 2(최근 30일)
Low Kay Tong 2022년 9월 15일
편집: Low Kay Tong 2022년 9월 15일
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!

댓글을 달려면 로그인하십시오.

### 답변(1개)

Stephen23 2022년 9월 15일
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)
T1 = 16×4 table
a b c d __ __ _ _ 11 22 2 2 33 44 2 4 55 66 2 6 77 88 2 8 99 12 4 2 23 34 4 4 45 56 4 6 67 78 4 8 89 98 6 2 87 76 6 4 65 54 6 6 43 32 6 8 10 21 8 2 1 2 8 4 3 4 8 6 5 6 8 8
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)
T2 = 6×2 table
cc dd ___ ___ 1.2 2.5 1.6 4.6 5.5 3.5 6.7 3.2 5.9 1.5 2.3 1.9
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,:)
T3 = 6×4 table
a b c d __ __ _ _ 11 22 2 2 33 44 2 4 99 12 4 2 23 34 4 4 89 98 6 2 87 76 6 4
##### 댓글 수: 1표시숨기기 없음
Low Kay Tong 2022년 9월 15일
I am sorry, but your output is different from my intended output. is it due to the lack of the absolute sign? I'd appreciate it if you could explain the code!

댓글을 달려면 로그인하십시오.

### 범주

Find more on Tables in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by