Written and maintained by CASRAI Editorial Board
Last updated
A matrix in MATLAB is a two-dimensional, rectangular array of data elements arranged in rows and columns (MathWorks, Creating and Concatenating Matrices, read 2026-11-07). This guide follows one small matrix through the tasks researchers and analysts do most often: create it, read single elements, print it, and transform it. Every command below comes from the current MathWorks documentation; the 2-by-2 example is simple enough to check by hand.
What is a matrix in code?
In code, a matrix is a named grid of values addressed by row and column position. In MATLAB, A(3,4) means the element in row 3, column 4 of A. MathWorks describes matrices and arrays as “the fundamental representation of information and data in MATLAB”, so nearly every dataset, image or set of model coefficients you handle will be stored this way.
What is an array in MATLAB, and how does it differ from a matrix?
An array is the general container; a matrix is the two-dimensional case. A single row of numbers is a 1-by-N row vector, a grid is a matrix, and MATLAB also supports multidimensional arrays and empty arrays (both are separate topics in the MathWorks “Matrices and Arrays” documentation, read 2026-11-07). In everyday MATLAB usage the two words are often interchangeable for 2-D data.
What it is used for in research
- Holding a data table of numbers: rows as observations, columns as variables. (For mixed-type tabular data with named columns, see MATLAB data tables.)
- Linear algebra and numerical modelling, where equations are written directly in matrix form.
- Repeating an operation across many rows or columns; see loops in MATLAB for when to iterate and when to operate on the whole matrix.
Install or access (official source only)
MATLAB is a MathWorks product. Obtain it, and check what your institution licenses, only from mathworks.com. The commands in this guide use core MATLAB and need no toolbox. If you do not have a licence, see the free alternatives table below.
Step-by-step: create, index, print and transform a matrix
How to create a matrix in MATLAB
- Type the values inside square brackets. Spaces or commas separate elements within a row, and semicolons separate rows:
A = [12 62; 93 -8]This produces a 2-by-2 matrix. Typing
A = [12 62 93 -8]instead produces a 1-by-4 row vector. - Or build a matrix of a given shape:
zeros(3,2)gives a 3-by-2 matrix of zeros andones(2,4)a 2-by-4 matrix of ones. The first argument is the number of rows and the second the number of columns.diag(v)places a vectorvon the diagonal. - Or generate a sequence with the colon operator:
1:10,0:2:10(step of 2) and6:-1:0(counting down) each create a row vector. - Check the result with
sz = size(A), which returns the number of rows and columns. ForAabove, that is 2 and 2.
How to combine matrices (concatenation)
Given two matrices A and B, C = [A B] joins them side by side (horizontal concatenation) and D = [A; B] stacks them (vertical concatenation). The function forms are horzcat, vertcat and cat. Horizontal joining needs the same number of rows; vertical joining needs the same number of columns.
How to read or change one element
Use round brackets with row first, then column. A(2,1) returns 93 for the matrix above. Assigning outside the current size, for example A(3,4) = 1, expands the matrix and pads the new positions with zeros (MathWorks, read 2026-11-07).
How to print a matrix in MATLAB
There are three ways, and the difference is whether the variable name is shown:
- Leave off the semicolon or type the variable name: MATLAB displays
A =followed by the values. disp(A)displays the value “without printing the variable name” (MathWorks, disp). If the array is empty, nothing is displayed.fprintfprints formatted text, for examplefprintf('%s will be %d this year.. The
',name,age)escape ends the line; without it the next output continues on the same line.
To label columns, print a header string before the matrix, as in the documented pattern disp(' Corn Oats Hay') followed by disp(X). To show several arrays on one line, combine them by concatenation, sprintf or fprintf.
Suppress the automatic display of any assignment by ending it with a semicolon, then print only what you want.
How to transform a matrix in MATLAB
- Transpose swaps rows and columns.
B = A.'(orB = transpose(A)) “interchanges the row and column index for each element”. The documentation’s example usesA = magic(4), whose first row 16 2 3 13 becomes the first column ofB. For our matrix,A.'is [12 93; 62 -8]. The operatorA'is the complex conjugate transpose: it also negates the sign of the imaginary part of complex elements, so for real data the two give the same result. - Reshape keeps the elements but changes the shape. With
A = 1:10;,B = reshape(A,[5,2])gives a 5-by-2 matrix whose first column is 1 to 5 and second column 6 to 10 (elements are read column by column). The size vector must contain at least 2 elements andprod(sz)must equalnumel(A). One dimension may be given as[]and MATLAB calculates it (MathWorks, reshape, read 2026-11-07). - Rearrange or remove:
permutereorders dimensions (ipermuteis its inverse) andsqueezeremoves dimensions of length 1. The “Matrices and Arrays” documentation also covers removing rows or columns. - Combine: concatenation, described above, is also a transformation.
Output and how to read it
Dimensions are always rows by columns. size(A) returning 2 2 means 2 rows and 2 columns; zeros(3,2) returns 3 rows and 2 columns. When a reshape or transpose seems to scramble data, check the order: MATLAB fills and reads column by column, as the 1:10 example shows.
Troubleshooting
- Rows come out as one long row. A space or comma separates elements in a row; a semicolon starts a new row.
[12 62 93 -8]is a row vector, while[12 62; 93 -8]is 2-by-2. - Concatenation fails with a dimension error. Horizontal joins need equal row counts and vertical joins need equal column counts. Run
sizeon each piece and compare before joining. - Reshape fails. The product of the new dimensions must equal the original element count (
prod(sz)equalsnumel(A)). Use[]for one dimension if you want MATLAB to work it out. - Nothing prints. A trailing semicolon suppresses display, and
dispof an empty array shows nothing. Check withisempty(A)orsize(A). - Printed output runs together.
fprintfneeds theescape to end a line.
Free alternatives
| Tool | What the official source says | Source |
|---|---|---|
| GNU Octave | Describes itself as a “Scientific Programming Language” with mathematics-oriented syntax; free software under the GNU General Public License; syntax “largely compatible with Matlab”, so some MATLAB scripts run unchanged and others need edits. | octave.org (read 2026-11-07) |
| NumPy (Python) | Open-source Python array library; functions such as numpy.sum take an axis argument to work along rows or columns. See also reversing a vector in NumPy. |
numpy.org documentation (read 2026-11-07) |
FAQ
How do I print out a matrix in MATLAB?
Type its name without a semicolon to see it with its name, use disp(A) to print the values alone, or use fprintf for formatted text.
What is a matrix in code?
A named, rectangular grid of values addressed by row and column, such as A(3,4).
What is an array in MATLAB?
The general data container. A matrix is a two-dimensional array; MATLAB also supports multidimensional and empty arrays.
How do I transform a matrix in MATLAB?
Use A.' to transpose, reshape to change shape, permute to reorder dimensions, and [A B] or [A; B] to join matrices.
How do I create a matrix in MATLAB?
Write values in square brackets with spaces or commas between columns and semicolons between rows, for example A = [12 62; 93 -8].
Sources
- MathWorks, Creating and Concatenating Matrices (read 2026-11-07).
- MathWorks, disp (read 2026-11-07).
- MathWorks, Matrices and Arrays (read 2026-11-07).
- MathWorks, transpose and reshape (read 2026-11-07).
- GNU Octave, octave.org (read 2026-11-07).







