# MATLAB Programming/Arrays/Introduction to Array Operations

[MATLAB Programming\|/MATLAB Programming]m]
Chapter 1: MATLAB ._.
    Fundamentals of MATLAB
    MATLAB Workspace
    MATLAB Variables
    *.mat files
Chapter 2: MATLAB Concepts
    MATLAB operator
    Data File I/O
Chapter 3: Variable Manipulation
    Numbers and Booleans
    Strings
    Portable Functions
    Complex Numbers
Chapter 4: Vector and matrices
    Vector and Matrices
    Special Matrices
    Operation on Vectors
    Operation on Matrices
    Sparse Matrices
Chapter 5: Array
    Arrays
    Introduction to array operations
    Vectors and Basic Vector Operations
    Mathematics with Vectors and Matrices
    Struct Arrays
    Cell Arrays
Chapter 6: Graphical Plotting
    Basic Graphics Commands
    Plot
    Polar Plot
    Semilogx or Semilogy
    Loglog
    Bode Plot
    Nichols Plot
    Nyquist Plot
Chapter 7: M File Programming
    Scripts
    Comments
    The Input Function
    Control Flow
    Loops and Branches
    Error Messages
    Debugging M Files
Chapter 8: Advanced Topics
    Numerical Manipulation
    Advanced File I/O
    Object Oriented Programming
    Applications and Examples
    Toolboxes and Extensions
Chapter 9: Bonus chapters
    MATLAB Benefits and Caveats
    Alternatives to MATLAB
    [MATLAB_Programming/GNU_Octave|What is Octave= (8) hsrmonic functions]
    Octave/MATLAB differences

## Introduction to array operations

As arrays are the basic data structure in MATLAB, it is important to understand how to use them effectively. See the previous section for that.
Arrays in MATLAB obey the same rule as their mathematical counterpart: by default, the matrix definitions of operations are used, unless a special operator called the dot operator is applied.
Because arrays operations are so similar to the equivalent mathematical operations, a basic knowledge of linear algebra is mandatory to use matlab effectively. However, we won't be as precise as in mathematics when using the terms vector and matrix. In MATLAB, both are arrays of doubles (thus being a matrix in the real mathematical meaning), and MATLAB considers vectors as a matrices with only one row or only one column. However, there are special functions just for vectors; see the vector module for an explanation of how to use these.

## Basics
### Accessing elements of a matrix
#### Using a Single Index

Now that you know how to define a simple array, you should know how to access its elements. Accessing the content of an array is done through the operator, with the index inside the parenthesis; the indexing of the first element is 1:
Accessing an element outside the bounds will result in an error:

#### Using Multiple Indexes

To access a single matrix element, you can use the (i,j) subscript, where i is the index in the row, and j in the column:

#### Using A Unique Index

You can also access a matrix element through a unique index; in this case, the order is column major, meaning you first go through all elements of the first column, then the 2d column, etc... The column major mode is the same as in Fortran, and the contrary of the order in the C language.

#### Using a Colon (:) for a Block Index

It is also possible to access blocks of matrices using the colon (:) operator. This operator is like a wildcard; it tells MATLAB that you want all elements of a given dimension or with indices between two given values. For example, say you want to access the entire first row of matrix a above, but not the second row. Then you can write:
Now say you only want the first two elements in the first row. To do this, use the following syntax:
The syntax a(:) changes a into a column vector (column major):

#### Using the end Operator

Finally, if you do not know the size of an array but wish to access all elements from a certain index until the end of the array, use the end operator, as in

### Logical Addressing

In addition to index addressing, you can also access only elements of an array that satisfy some logical criterion. For example, suppose a = [1.1, 2.1, 3.2, 4.5] and you only want the values between 2 and 4. Then you can achieve this in two ways. The first is to use the find function to find the indices of all numbers between 2 and 4 in the array, and then address the array with those indices:
This does not work in MATLAB 2006b.
The second method is to use logical addressing, which first changes a into a logical array, with value 1 if the logical expression is true and 0 if it is false. It then finds and returns all values in the a which are true. The syntax for this is as follows:

## Basic operations
### Rational Operators on Arrays
#### Addition and Subtraction

The interesting part is of course applying some operations on those arrays. You can for example use the classic arithmetic operations + and - on any array in matlab: this results in the vector addition and subtraction as defined in classic vector vectors spaces $\mathbb {R} ^{n}$, which is simply the addition and subtraction elements wise:

#### Multiplication by a Scalar

The multiplication by a scalar also works as expected:

#### Multiplying and Dividing Arrays

Multiplication and division are more problematic: multiplying two vectors in $\mathbb {R} ^{n}$ does not make sense. It makes sense only in the matrix context. Using the symbol * in matlab computes the matrix product, which is only defined when the number of columns of the left operand matches the number of rows of the right operand:
Using the division symbol / has even more constraints, as it imposes the right operand to be invertible (see Wikipedia:Invertible matrix). For square matrices, $a/b$ is equivalent to $a*b^{-1}$. For example :

#### Component-wise Operations

If you desire to multiply or divide two matrices or vectors component-wise, or to raise all components of one matrix to the same power, rather than using matrix definitions of these operators, you can use the dot (.) operator. The two matrices must have the same dimensions. For example, for multiplication,
The other two componentwise operators are ./ and .^.
As matlab is a numerical computing language, you should keep in mind that a matrix which is theoretically invertible may lead to precision problems and thus giving imprecise results or even totally wrong results. The message above "matrix is singular to working precision" should appear in those cases, meaning the results cannot be trusted.
Non-square matrices can also be used as the right operand of /; in this case, it computes the pseudoinverse. This is especially useful in least square problems.

#### Transpose

A transpose of a matrix is taken using .'

### Boolean Operators on Arrays

The same boolean operators that can be used for point values can also be used to compare arrays. To do this, MATLAB compares the elements componentwise and returns them in a logical array of the same size as the two arrays being compared. The two arrays must have the same size. For example,
You must be careful when using comparisons between arrays as loop conditions, since they clearly do not return single values and therefore can cause ambiguous results. The loop condition should be reducable to a single boolean value, T or F, not an array. Two common ways of doing this are the "any" and the "all" functions. A function call any(array) will return true if array contains any nonzero values and false if all values are zero. It does the comparisons in one direction first then the other, so to reduce a matrix you must call the any function twice. The function all, similarly, returns true if and only if all elements in a given row or column are nonzero.

### Concatenating Arrays

Concatenating arrays involves sticking arrays together.

##### Horizontal Concatenating

Horizontal concatenation is done by treating an array as if it were a variable included in a row.

##### Vertical Concatenating

Vertical concatenation is done by treating an array as if it were a variable included in a column.

## Solving Linear Systems

To solve a linear system in the form Ax = b use the "\" operator.
Example: