A simple array is defined using the colon syntax: "init"codice_2"increment"codice_2"terminator". For instance:

defines a variable named codice_4 (or assigns a new value to an existing variable with the name codice_4) which is an array consisting of the values 1, 3, 5, 7, and 9. That is, the array starts at 1 (the "init" value), increments with each step from the previous value by 2 (the "increment" value), and stops once it reaches (or to avoid exceeding) 9 (the "terminator" value).

the "increment" value can actually be left out of this syntax (along with one of the colons), to use a default value of 1.

assigns to the variable named codice_6 an array with the values 1, 2, 3, 4, and 5, since the default value of 1 is used as the incrementer.

Indexing is one-based, which is the usual convention for matrices in mathematics, although not for some programming languages such as C, C++, and Java.

Matrices can be defined by separating the elements of a row with blank space or comma and using a semicolon to terminate each row. The list of elements should be surrounded by square brackets: []. Parentheses: () are used to access elements and subarrays (they are also used to denote a function argument list).

Sets of indices can be specified by expressions such as "2:4", which evaluates to [2, 3, 4]. For example, a submatrix taken from rows 2 through 4 and columns 3 through 4 can be written as:

A square identity matrix of size "n" can be generated using the function "eye", and matrices of any size with zeros or ones can be generated with the functions "zeros" and "ones", respectively.
Most MATLAB functions can accept matrices and will apply themselves to each element. For example, codice_7 will multiply every element in "J" by 2, and then reduce each element modulo "n". MATLAB does include standard "for" and "while" loops, but (as in other similar applications such as R), using the vectorized notation often produces code that is faster to execute. This code, excerpted from the function "magic.m", creates a magic square "M" for odd values of "n" (MATLAB function codice_8 is used here to generate square matrices I and J containing 1:n).

Vectors/matrices
Alternatives
License
Release history