# MATLAB Programming/Arrays

[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 Arrays

An array is the most fundamental data type in MATLAB. In MATLAB, as in many traditional languages, arrays are a collection of several values of the same type. The string and number data type formerly presented are particular cases of arrays.
A matrix is an array with two dimensions. Most arrays have the same data type; however, a cell array is an array with varying data types. If no data type is specified for numbers, then the default data type is the equivalent to the double precision floating point in the C programming language on the same architecture. For example on x86 and PowerPC a double has 64 bits.

## Declaring Arrays
### Row and Column Arrays

A row array is created using comma separated values inside brackets:
Sometimes commas are omitted for simple row arrays. Omitting commas is not recommended because in larger more complex arrays white-spaces can be ambiguous. Commas almost always indicate an array is horizontal.
A column array is created using semicolons to separate values:

### Declaring multi-dimensional arrays

A two dimensional array (or a matrix) is declared with commas separating columns, and semicolons separating rows:
Simple matrix manipulation is the basis of many linear algebra computations. To use arrays of more than two dimensions, a matrix has to be extended using indexing.
When declaring arrays in MATLAB all rows and all columns need have same size. If not an error message will be generated:

## Indexing Arrays

Arrays are indexed using integers. To return a single element in a simple array, use a single integer.
To return a single element in a two dimensional array one number as a row index and the second as a column index.
To return multiple elements in an array an array can be used as an index.
To return the last element of an array use (end).
A range of indexes can be returned using a colon(:)

## Properties of MATLAB arrays and matrices

Contrary to low level languages such as C, an array in MATLAB is a more high level type of data: it contains various information about its size, its data type, and so on.
The number of rows and columns of the matrix can be known through the built-in size function. Following the standard mathematical convention, the first number is the number of rows and the second is the number of columns:
The goal of MATLAB arrays is to have a type similar to mathematical vectors and matrices. As such, row and column arrays are not equivalent. Mono-dimensional arrays are actually a special case of multi-dimensional arrays, and the 'size' function can be used for them as well.
Row and column do not have the same size, so they are not equivalent:

## Why Use Arrays?

A major advantage of using arrays and matrices is that it lets you avoid using loops to perform the same operation on multiple elements of the array. For example, suppose you wanted to add 3 to each element of the array [1,2,3]. If MATLAB didn't use arrays you would have to do this using a FOR loop:
Doing this is not efficient in MATLAB, and it will make your programs run very slowly. Instead, you can create another array of 3s and add the two arrays directly. MATLAB automatically separates the elements:
If all you are doing is adding a constant, you can also omit the declaration of 'arrayofthrees', as MATLAB will assume that the constant will be added to all elements of the array. This is very useful, for example if you use an array with variable size:
The same rule applies to scalar multiplication.
See Introduction to array operations for more information on the operations MATLAB can perform on arrays.
Arrays are a fundamental principle of MATLAB, and almost everything in MATLAB is done through a massive use of arrays. To have a deeper explanation of arrays and their operations, see Arrays and matrices.