Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

Matrix in MATLAB: Create, Print and Transform

How to create, print and transform a matrix in MATLAB, with commands checked against the MathWorks documentation and free alternatives.

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

  1. 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.

  2. Or build a matrix of a given shape: zeros(3,2) gives a 3-by-2 matrix of zeros and ones(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 vector v on the diagonal.
  3. Or generate a sequence with the colon operator: 1:10, 0:2:10 (step of 2) and 6:-1:0 (counting down) each create a row vector.
  4. Check the result with sz = size(A), which returns the number of rows and columns. For A above, 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.
  • fprintf prints formatted text, for example fprintf('%s will be %d this year.
    ',name,age)
    . The
    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.' (or B = transpose(A)) “interchanges the row and column index for each element”. The documentation’s example uses A = magic(4), whose first row 16 2 3 13 becomes the first column of B. For our matrix, A.' is [12 93; 62 -8]. The operator A' 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 and prod(sz) must equal numel(A). One dimension may be given as [] and MATLAB calculates it (MathWorks, reshape, read 2026-11-07).
  • Rearrange or remove: permute reorders dimensions (ipermute is its inverse) and squeeze removes 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 size on each piece and compare before joining.
  • Reshape fails. The product of the new dimensions must equal the original element count (prod(sz) equals numel(A)). Use [] for one dimension if you want MATLAB to work it out.
  • Nothing prints. A trailing semicolon suppresses display, and disp of an empty array shows nothing. Check with isempty(A) or size(A).
  • Printed output runs together. fprintf needs the
    escape 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

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Matrix in MATLAB: Create, Print and Transform

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →