Arrays in MATLAB

Arrays in MATLAB

Array is a fundamental form that MATLAB uses to store and manipulate data. An array is a list of numbers arranged in rows or columns.

There are two types of arrays

  • Simples Array ( One-dimensional Arrays)
  • Complex Array ( Two-dimensional Arrays)

One dimensional array frequently represent vectors and Two-dimensional array represent matrices.

Creating Vector

The vector is created by typing numbers in square brackets

variable_name = [ here type vector elements ]

Row Vector

To create row vector, type comma or space after element inside square brackets

In this example, There are 9 elements in one_dimensional row vector
>> one_dimensional = [1 2 3 4 5 6 7 8 9]

Column Vector

To create column vector, type semicolon after every element inside square brackets

In this example, There are 9 elements in one_dimensional column vector
>> one_dimensional = [1; 2; 3; 4; 5; 6; 7; 8; 9]

Creating a vector with constant spacing in specific range

In this vector space(magnitude) between each element of vector is same

variable_name = [p:q:r]      or     variable_name = p:q:r

where p is starting limit, q is interval or spacing and r is upper limit of function

variable = [5:5:25]

In this example,  variable = 5, 10, 15, 20, 25

Creating a vector with equal spacing in specific range by specifying limits and total number of element

In this we specify upper and lower limit and number of elements, then function calculates interval or spacing between elements and assigns value to vector.

variable_name = linspace(xi, xf, n)

where xi is lower limit, xf is upper limit and n is total number of elements

variable = linspace(5,25,5)

In this example, variable = 5, 10, 15, 20, 25

Two-dimensional Arrays

Two-dimensional array consist of rows and columns. All row elements are seperated with space after each row place semicolon for next row and so on.

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

In this example:

variable = 1  2  3
4  5  6
7  8  9

Utility Commands for Matrices

  • zeros(m,n)

It creates a matrix with m rows and n columns in which all elements are zero

  • ones(m,n)

It creates a matrix with m rows and n columns in which all elements are ones

  • eye(n)

It creates an identity matrix with n rows and columns

The Transpose Operator ( ‘ )

Transpose operator is used to change rows into columns

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

In this example value is:
var = 1 4 7
2 5 8
3 6 9

Vector and Matrix Positioning

Elements in array can be accessed individually, For vector named  vtr, vtr(k) refers to the element in position k.

vtr = [1 2 3; 4 0 6; 7 8 9]
vtr(5) value is 5, we can assign value to 5th value with equal operator i.e. vtr(5) = 55

The address of an element in a matrix is its position, defined by the row and the column number where it is located

vtr = [1 2 3; 4 0 6; 7 8 9]
vtr(1,2) value is 2
vtr(1,2) = 22 , new value is assigned

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s