Matlab Arrays

From Sutherland_wiki
Revision as of 06:25, 29 July 2008 by 00033394 (talk | contribs) (Array Basics)
Jump to: navigation, search


Array Basics

An array is an n-dimensional collection of numbers. Matrices are 2-dimensional arrays, and vectors are 1-dimensional arrays.

Vectors can be either a row or column vector:


  \quad
  a=\left[ \begin{array}{c} a_1 \\ a_2 \\ a_3 \\ \vdots \\ a_n \end{array} \right]
  \quad
  b = \left[ \begin{array}{cccc} b_1  &  b_2  &  \cdots & b_n \end{array} \right]
  \quad
  c = \left[ \begin{array}{c} 1 \\ 4 \\ 3.2 \\ \pi \end{array} \right]

We refer to the elements of an array by their position in the array. For example, the third element in c is 3.2, and the second element in a is a_2.

Matrices are two-dimensional arrays, whose elements are referred to by the row and column that they belong to. For example,


  \quad
  A = \left[ \begin{array}{cc} a_{11} & a_{12} \\ a_{21} & a_{22} \\ a_{31} & a_{32} \end{array}\right]

is a matrix with three rows and two columns. We refer to this as a \scriptstyle 3 \times 2 matrix. The first index is the row and the second index is the column.

Transposing Arrays

Occasionally we want to transpose an array. This is done by exchanging rows and columns. For example,


  A = \left[ \begin{array}{cc} 2 & 4 \\ 6 & 8 \\ 10 & 12 \end{array} \right]
  \quad \Rightarrow \quad
  A^\mathsf{T} = \left[ \begin{array}{ccc} 2 & 6 & 10 \\ 4 & 8 & 12 \end{array} \right]

Here the ^\mathsf{T} superscript indicates transpose. Note that if we transpose an array twice, we recover the original array. In other words, \left(A^\mathsf{T}\right)^\mathsf{T} = A.

Creating Vectors

Manually Creating Vectors

Using the ":" operator to create arrays

Linspace and Logspace

Creating Matrices

Manually Creating Matrices

Sparse Matrices

Accessing Arrays

Indexing arrays

Slicing arrays using the ":" operator