table2array - Convert table to homogeneous array - MATLAB (original) (raw)
Convert table to homogeneous array
Syntax
Description
A = table2array([T](#btx30z0-1-T))
converts an input table or timetable to a homogeneous array. The variables in the input T
become columns in the output array A
.
The output A
does not include the table properties inT.Properties
.
- If
T
is a table with row names, thenA
does not include the row names. - If
T
is a timetable, thenA
does not include the row times.
Examples
Create a table, T
, consisting of numeric data.
T = table([1;2;3],[2 8; 4 10; 6 12],[3 12 21; 6 15 24; 9 18 27],... 'VariableNames',["One" "Two" "Three"])
T=3×3 table
One Two Three
___ _______ _____________
1 2 8 3 12 21
2 4 10 6 15 24
3 6 12 9 18 27
Convert table, T
, to an array.
A = 3×6
1 2 8 3 12 21
2 4 10 6 15 24
3 6 12 9 18 27
A
contains two columns from variable Two
and three columns from variable Three
.
Define the numeric subset of a table to convert to an array.
Create a table with nonnumeric data in the first variable.
T = table(categorical(["Y";"Y";"N";"N";"F"]),[38;43;38;40;49],... [71;69;64;67;64],[176;163;131;133;119],... 'VariableNames',["Smoker" "Age" "Height" "Weight"])
T=5×4 table Smoker Age Height Weight ______ ___ ______ ______
Y 38 71 176
Y 43 69 163
N 38 64 131
N 40 67 133
F 49 64 119
Convert T(:,2:4)
to an array.
A = table2array(T(:,2:4))
A = 5×3
38 71 176
43 69 163
38 64 131
40 67 133
49 64 119
A
does not include data from the variable Smoker
.
Create a table, T
, with two rows and three variables where each variable has three dimensions.
T = table(ones(2,1,3),2ones(2,2,3),3ones(2,3,3),... 'VariableNames',["One" "Two" "Three"])
T=2×3 table
One Two Three
____________ ____________ ____________
1×1×3 double 1×2×3 double 1×3×3 double
1×1×3 double 1×2×3 double 1×3×3 double
The size of the table is 2-by-3.
Convert table T
to an array.
A = A(:,:,1) =
1 2 2 3 3 3
1 2 2 3 3 3
A(:,:,2) =
1 2 2 3 3 3
1 2 2 3 3 3
A(:,:,3) =
1 2 2 3 3 3
1 2 2 3 3 3
The size of A
is 2-by-6-by-3.
Input Arguments
Input table, specified as a table or timetable. All variables in T
must have sizes and data types that are compatible for horizontal concatenation. Specifically, the size of all variable dimensions greater than2
must match.
- If
T
is anm
-byn
table or timetable with variables that each have one column, then each variable becomes one column inA
, andA
is anm
-by-n
array. - If
T
contains variables that consist of more than one column, those variables become multiple columns inA
, and the size ofA
is greater than the size ofT
. - If
T
contains variables with more than two dimensions, the number of dimensions ofA
is the same as the number of variable dimensions.
Tips
table2array
horizontally concatenates the variables inT
to createA
. If the variables inT
are cell arrays,table2array
does not concatenate their contents, andA
is a cell array, equivalent totable2cell(T)
. To create an array containing the contents of variables that are all cell arrays, usecell2mat(table2cell(T))
.- These syntaxes return the same array as
table2array(T)
.T{:,:}
T.Variables
, when the name of the second dimension of the table is the default name,'Variables'
T.(T.Properties.DimensionNames{2})
, when the name of the second dimension of the table is not'Variables'
Algorithms
If T
contains variables with different data types that are compatible for horizontal concatenation, table2array
creates a homogeneous array, A
, of the dominant data type. For example, if T
contains double
and single
numeric data, table2array(T)
returns an array with data type single
.
Extended Capabilities
Thetable2array
function fully supports tall arrays. For more information, see Tall Arrays.
Version History
Introduced in R2013b