I want to merge the content of two tables with identical variables, but for a given key variable value the columns 1:N in table 1 are filled and columns N+1:end in the other.
조회 수: 2 (최근 30일)
이전 댓글 표시
This is what I would like to achieve: A = table({'a'}, 5, 15, 25, 0, 0, 0,'VariableNames',{'A1','A2', 'A3', 'A4', 'B1', 'B2', 'B3'}) A = A1 A2 A3 A4 B1 B2 B3 _
'a' 5 15 25 0 0 0
>> B = table({'a'}, 0, 0, 0, 6, 12, 18,'VariableNames',{'A1','A2', 'A3', 'A4', 'B1', 'B2', 'B3'}) B = A1 A2 A3 A4 B1 B2 B3 _
'a' 0 0 0 6 12 18
>> What operation would yield table C (matching the values of variable 'A1'): C = table({'a'}, 5, 15, 25, 6, 12, 18,'VariableNames',{'A1','A2', 'A3', 'A4', 'B1', 'B2', 'B3'}) C =
A1 A2 A3 A4 B1 B2 B3
___ __ __ __ __ __ __
'a' 5 15 25 6 12 18
댓글 수: 0
답변 (1개)
Ramnarayan Krishnamurthy
2017년 9월 21일
편집: Ramnarayan Krishnamurthy
2017년 9월 21일
Merging 2 tables on the basis of key variables and retaining only the common rows can be achieved using the 'innerjoin' function.
For the requirements in this question, we would need to manipulate the input data A and B, so that only the desired columns from both the tables are selected
t = innerjoin(A(:,[1:4]),B(:,[1,5:end]),'LeftKeys',1,'RightKeys',1);
t =
A1 A2 A3 A4 B1 B2 B3
___ __ __ __ __ __ __
'a' 5 15 25 6 12 18
Additionally, to generalize the above code where N is the number of filled columns in Table 1:
N = 4;
t = innerjoin(A(:,[1:N]),B(:,[1,N+1:end]),'LeftKeys',1,'RightKeys',1);
The documentation for the 'innerjoin' function is available at: https://www.mathworks.com/help/matlab/ref/innerjoin.html
Hope this helps!
댓글 수: 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!