Matlab

How to Convert a Table to a Matrix in MATLAB?

In MATLAB, we deal with both a table and a matrix for storing the data in the form of rows and columns. A table and a matrix are almost similar, but a matrix must have all the elements of the same data type however, a table can have different types of data but all the elements in a column must be of the same data type. MATLAB supports the conversion of a table to a matrix using a built-in table2array() function. In this article, we are going to demonstrate how to convert a table to a matrix in MATLAB using the table2array() function.

How to Use the table2array() Function in MATLAB for Converting a Table to a Matrix?

The table2array() is a built-in function in MATLAB that allows us to convert a table to a 2-d homogenous array or a matrix. This function accepts the table T as an argument and returns a matrix containing the table data in the matrix form. The function follows a simple syntax that is given below:

A = table2array(T)

Here:

The expression A = table2array(T) creates an array A whose type depends on the contents of T. The sizes and types of each variable in T must be compatible with the horizontal concatenation.

  • If T is an M by N table and each variable has its own column, then A will have a column for each variable in T.
  • If T represents a table having row names, then A excludes the names of the rows.
  • If T represents a timetable, then a excludes the row timings.

Example

This MATLAB code first creates a table having numeric data and then convert it into a matrix using the table2array() function.

T = table([1;2;3],[3 7; 10 14; 8 19],[7 18 25; 9 17 29; 10 19 29],...

'VariableNames',["One" "Two" "Three"])

A = table2array(T)

Conclusion


In MATLAB, we deal with both a table and a matrix for storing the data in the form of rows and columns. A table and a matrix are almost similar, but a matrix must have all the elements of the same data type however, a table can have different types of data but all the elements in a column must be of the same data type. MATLAB supports the conversion of a table to a matrix using a built-in table2array() function. This tutorial described the use of the table2array() function for converting a table to a matrix.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.