# Matlab Matrices and Arrays

##### Introduction to Arrays and Vectors

In this article we are going discuss about arrays and vectors of numbers and mathematical operations on them. Arrays are the fundamental representation of information and data in matlab. A matrix is a two dimensional array and a vector is a one dimensional array.

##### How to declare a Array

As mentioned above, array can be divided into two parts

Matrix

- A matrix is a two dimensional array.
- Syntax to declare a matrix

Y = [a1 a2 a3; b1 b2 b3]

Y = [a1 a2 a3; b1 b2 b3] = a1 a2 a3 b1 b2 b3

- To create columns in matrix you need give space or comma between two elements as mentioned in above syntax
- To terminate a row you need to type a semicolon as mentioned in above syntax

**Vector**

- Vector is a one dimensional array.
- Syntax to declare a matrix

Y = [a1 a2 a3]

Y = [a1 a2 a3] = a1 a2 a3

##### Arrays

An array can be defined as ” An array is a list of numbers or expressions arranged in horizontal rows and vertical columns”

##### Special arrays functions in matlab

In all these arrays a single argument creates a square array and two arguments creates a rectangular array. These functions are as follows

**Zeros()**

This function creates a array of all zeros i.e all elements of array will be zero.

y = zeros

It will produce a scalar zero.

y = zeros(n)

It will produce a n*n matrix, as follows

y = zeros = 0 y = zeros(3) = 0 0 0 0 0 0 0 0 0

**Ones()**

This function creates a array of all one i.e all element of array will be one

y = ones

It will produce a scalar one.

y = ones(n)

It will produce a n*n matrix, as follows

y = ones(m,n)

It will produce a m*n matrix, as follows

y = ones(sz)

Syntax ones(sz) is used to create a array of ones that is same size as existing array. See below in box.

y = ones = 1 y = ones(3) = 1 1 1 1 1 1 1 1 1 y = ones(3,4) = 1 1 1 1 1 1 1 1 1 1 1 1 Define a array A of 3*4 dimension A = [1 2 3 4; 5 6 7 8; 9 0 1] sz = size(A) = 3 (No. of rows) 4 (No. of columns) Create an array of ones that is the same size as <code>A</code> y = ones(sz) = 1 1 1 1 1 1 1 1 1 1 1 1

**Eye()**

This function creates a diagonal matrix with diagonal element’s value one i.e it will create a identity matrix.

y = eye

It will produce a scalar one.

y = eye(n)

It will produce a n*n identity matrix as mentioned in below box

y = eye (n,m)

It will produce an n*m matrix with ones on the main diagonal and zeros elsewhere as mentioned in below box

y = eye = 1 y = eye(3) = 1 0 0 0 1 0 0 0 1 y = eye(2,3) = 1 0 0 0 1 0

**Diag**

This function is used to create a diagonal matrix of vector A.

y = diag(A)

Where A is a vector

It will produce a square diagonal matrix with the elements of vector A on the main diagonal. As mentioned in below box

y = diag(X)

Where X is matrix

It will produce a column matrix with the diagonal elements of a matrix A. As mentioned in below box

First you need to define a vector as follows A = [1 2 3 2 4] y = diag(A) = 1 0 0 0 0 0 2 0 0 0 0 0 3 0 0 0 0 0 2 0 0 0 0 0 4

##### Multidimensional array

Multidimensional array can be defined as “An array that has more than two dimensions is known as multidimensional array”. Multidimensional arrays in matlab are an extension of the normal two-dimensional matrix.

Let’s consider an example to explain multidimensional array

*Step1*

Consider a two dimensional array, as follows

A = [1 2 3; 4 5 6; 7 8 9] = 1 2 3 4 5 6 7 8 9

*Step2*

As per our definition multidimensional array has more than two dimension, where you have two dimension in your array (*step1*). So you need to extend it in one more direction as follows

- Suppose array A represents surface ABCD labelled as “1”
- Now you need extend it towards surface EFGH labelled as “2”

To extend this array towards surface “2”

A(:,:,2) = [1 2 4; 3 5 6; 9 8 7] Lets first : represents dimension towards x axis second : represents dimension towards y axis Third 2 represents third dimension toward surface "2" A(:,:,1) = [1 2 3; 4 5 6; 7 8 9] %% along suraface "1" = 1 2 3 4 5 6 7 8 9 A(:,:,2) = [1 2 4; 3 5 6; 9 8 7] = 1 2 4 3 5 6 9 8 7

Above array is an example of 3 dimensional array.

You can extent this array in more dimension using same assign operator as follows

A(:,:,1,2) = [1 2 3; 4 6 5; 9 8 7] A(:,:,2,2) = [9 8 7; 6 3 4; 5 2 1]

##### Array functions

You need to type name of array in () with following functions like

A = [1,2,3,5,6,7,8,9] length(A) Result will be = 8

Function | Action |
---|---|

length | Length of vector or largest array dimension |

ndims | Number of array dimensions |

numel | Number of array elements |

size | Array dimensions |

iscolumn | Determines whether input is column vector |

isempty | Determines whether array is empty |

ismatrix | Determines whether input is matrix |

isrow | Determines whether input is row vector |

isscalar | Determines whether input is scalar |

isvector | Determines whether input is vector |

blkdiag | Constructs block diagonal matrix from input arguments |

circshift | Shifts array circularly |

ctranspose | Complex conjugate transpose |

diag | Diagonal matrices and diagonals of matrix |

flipdim | Flips array along specified dimension |

fliplr | Flips matrix from left to right |

flipud | Flips matrix up to down |

ipermute | Inverses permute dimensions of N-D array |

permute | Rearranges dimensions of N-D array |

repmat | Replicates and tile array |

reshape | Reshapes array |

rot90 | Rotates matrix 90 degrees |

shiftdim | Shifts dimensions |

issorted | Determines whether set elements are in sorted order |

sort | Sorts array elements in ascending or descending order |

sortrows | Sorts rows in ascending order |

squeeze | Removes singleton dimensions |

transpose | Transpose |

vectorize | Vectorizes expression |

I am pleased to inform that you can also write articles for PaperGuide. Visit our page Join PaperGuide for more details or send your detailed resume along with a sample article ( atleast 1000 words ) at admin@paperguide.org.

Normally I do not learn post on blogs, however I

would like to say that this write-up very compelled me to check out and do so!

Your writing style has been surprised me. Thanks, quite great post.

Hi,

Thanks for your valuable time and appreciation. In near future we are coming up with new categories like Latex, kile and CSS etc. We are also adding small video and gif images to make it more understandable to users. So keep visiting http://www.paperguide.org

Thanks!

I simply want to mention I am just newbie to weblog and definitely liked this website. Very likely I’m planning to bookmark your site . You amazingly come with tremendous article content. Thanks a bunch for revealing your web site.