Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 45 ยท Matrices and Arrays

Everything Is a Matrix

Understand why everything in MATLAB and Octave is a matrix: scalars, row and column vectors, creating matrices, size, numel, transpose and 1-based indexing.

In most languages a number is a number and a list is something different. In MATLAB and Octave there is only one basic shape of data: a rectangular grid of values called a matrix (or array). A single number is a 1-by-1 grid, a list of numbers is a grid with one row or one column, and a table of numbers is a grid with several of each. Once this clicks, the rest of the language makes sense: the same operators and functions work on every shape.

This lesson shows how to create matrices, how to ask about their size, how to flip them with the transpose, and how to read single elements using MATLAB's one-based indexing. The real code is read-only MATLAB/Octave; the Python stand-ins use NumPy to reproduce the same results so you can experiment.

Same grid, different shapes

Scalar 1 by 1. One value.
Row vector 1 by N. Values side by side.
Column vector N by 1. Values stacked.
Matrix M rows by N columns.

Rows first, columns second, always.

1 / 2

Scalars, vectors and matrices

You build a matrix with square brackets. Spaces or commas separate elements in a row, and semicolons start a new row.

MATLAB
s = 7;                  % scalar, 1x1
r = [1 2 3 4];          % row vector, 1x4
c = [1; 2; 3];          % column vector, 3x1
A = [1 2 3; 4 5 6];     % matrix, 2x3

Typing A without a semicolon shows the grid. MATLAB right-aligns the columns:

MATLAB
>> A = [1 2 3; 4 5 6]

A =

     1     2     3
     4     5     6

Octave prints the same numbers with slightly narrower spacing, which is only a display difference.

Every row must have the same number of elements. This fails with an error about "dimensions" because the second row is shorter:

MATLAB
>> bad = [1 2 3; 4 5]
Error using vertcat
Dimensions of arrays being concatenated are not consistent.

A scalar really is a matrix. In MATLAB size(7) returns 1 1, and after s = 7; the expression s(1) is valid. Text is a matrix too: a character array is a row of characters, which Strings and Character Arrays explains later.

Asking about shape

Four functions describe an array:

Function Returns For A = [1 2 3; 4 5 6]
size(A) [rows columns] 2 3
numel(A) total number of elements 6
length(A) the largest dimension 3
ndims(A) number of dimensions 2
MATLAB
>> [m, n] = size(A)
m =
     2
n =
     3
>> size(A, 1)       % just the number of rows
ans =
     2

size can also return each dimension into its own variable, as in [m, n] = size(A). Use numel when you want the count of elements; length is convenient for vectors but can surprise you on matrices because it returns only the larger side.

Here is the same set of questions asked in NumPy. In NumPy shape gives the size, size counts elements and ndim counts dimensions. A NumPy 1-D array has no row or column orientation, so the stand-in uses 2-D arrays to match MATLAB.

Python Shapes of scalars, vectors and matrices (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The transpose

The transpose swaps rows and columns. In MATLAB and Octave you write it with an apostrophe:

MATLAB
>> r = [1 2 3];
>> c = r'
c =

     1
     2
     3

>> A'              % 3x2 version of the 2x3 matrix
ans =

     1     4
     2     5
     3     6

Turning a row into a column is something you will do constantly, for example when a function expects column data. For complex numbers ' is the conjugate transpose and .' is the plain transpose. For real data the two are identical; Arithmetic: Matrix vs Element-wise comes back to this.

In NumPy, .T transposes a 2-D array.

Python Transposing (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

One-based indexing

MATLAB and Octave number elements starting from 1. The first element of a vector is v(1) and the last of a vector with five elements is v(5). For a matrix you give the row first, then the column: A(2, 3) is the element in row 2, column 3.

MATLAB
>> v = [10 20 30 40 50];
>> v(1)
ans =
    10
>> v(5)
ans =
    50
>> A = [1 2 3; 4 5 6];
>> A(2, 3)
ans =
     6
>> A(1, 2) = 99    % assignment works the same way
A =

     1    99     3
     4     5     6

You can also use one number on a matrix. This linear index counts down the columns first, because MATLAB stores matrices column by column. In A above, A(1) is 1, A(2) is 4, A(3) is 99 (it was 2, now changed). We cover this in Indexing and Slicing.

An index of zero is an error in MATLAB: v(0) reports that indices must be positive integers. Going past the end on a read also fails ("Index exceeds the number of array elements"). Writing past the end is allowed and grows the array, which a later lesson covers.

Warning: Python, C, Java and JavaScript start at 0. If you translate between languages, shift every index by one. A(2, 3) in MATLAB is A[1, 2] in NumPy.

The stand-in below shows the translation. It reads the same elements MATLAB would, using zero-based NumPy indexes, and keeps a helper that accepts MATLAB-style one-based subscripts.

Python MATLAB subscripts in NumPy (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The order="F" argument tells NumPy to read down columns first, which is how MATLAB lays out memory. Plain NumPy arrays go along rows ("C" order) instead.

Putting it together

A few patterns you will use all the time:

MATLAB
x = [3 1 4 1 5];
n = numel(x);        % how many elements
last = x(end);       % the last element, 5
col = x';            % turn the row into a column
M = [x; 2*x];        % stack two rows into a 2x5 matrix

The keyword end inside an index means "the last position along that dimension", so x(end) works for a vector of any length. You will see more of it in the indexing lesson.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the size of A = [1 2 3; 4 5 6]?

    The semicolon starts a new row, so there are 2 rows with 3 columns each. Size is always written rows by columns.
  2. 2. What does r' do to a row vector r?

    The apostrophe is the transpose operator. It swaps rows and columns, so a 1-by-N row becomes N-by-1.
  3. 3. Which expression gives the first element of the vector v in MATLAB?

    Indexing starts at 1 and uses parentheses. v(0) is an error.
  4. 4. For A = [1 2 3; 4 5 6], what do numel(A) and length(A) return?

    numel counts all six elements, while length returns the largest dimension, which is 3.
  5. 5. The MATLAB element A(2, 3) corresponds to which NumPy expression?

    NumPy is zero-based and also uses (row, column) order, so MATLAB row 2, column 3 is A[1, 2].

Summary

  • Every value is a matrix: a scalar is 1x1, a vector is 1xN or Nx1.
  • Build matrices with [ ]: spaces or commas between columns, semicolons between rows, and all rows the same length.
  • size, numel, length and ndims describe the shape; sizes are rows by columns.
  • The apostrophe ' transposes: rows become columns.
  • Indexing starts at 1 and uses parentheses: A(row, column). end means the last position.
  • NumPy stand-ins need zero-based subscripts, so shift every index by one.

Next, create vectors and special matrices quickly in Ranges and Special Matrices.