Convert table with many rows to table with single row with data in sequence
조회 수: 19 (최근 30일)
이전 댓글 표시
What is an efficient way to assign values from one table (many rows and columns) to a table with one row.
Table 1: 3 variables with 10 entries each (10 rows x 3 columns) - VarNames: Start, End, Time & all values are numbers
Table 2: I want to extract data from Table 1 and input the values into Table 2 - VarNames: Start1, End1, Time1, Start2, End2, Time2, ... Start10, End10, Time10
For example:
The goal is to create something like below so that every column in Table2 doesn't need to have its own line of code; but I'm not sure if that's possible or realistic to do.
T2 = table();
x = 1;
while x<11
T2.Start(x) = T1(x,1);
T2.End(x) = T1(x,2);
T2.Time(x) = T1(x,3);
x = x+1;
end
댓글 수: 0
채택된 답변
Cris LaPierre
2022년 8월 17일
A bit convoluted, but should work.
% Create first table
Start = (11:10:101)';
End = Start+1;
Time = End+1;
T1 = table(Start,End,Time)
% Turn table into an array and transpose
d = table2array(T1)'
% create new variable names
varnm = ["Start";"End";"Time"] + (1:length(d));
% Build new table
T2 = array2table(d(:)','VariableNames',varnm(:))
댓글 수: 3
Cris LaPierre
2022년 8월 18일
Concatenate the two tables.
% First table
ParticipantNum = 1;
TrialNum = 15;
AvgTime = 7;
T1 = table(ParticipantNum,TrialNum,AvgTime)
% Second table
Start1 = 1;
End1 = 2;
Time1 = 3;
T2 = table(Start1,End1,Time1)
% Concatenate the tables together (must have same number of rows)
T3 = [T1,T2]
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!