Pages

Wednesday 6 March 2013

Array in MATLAB

 

The array is a fundamental form that MATLAB uses to store and manipulate data. An array is a list of numbers arranged in rows and or columns. The simplest array (one- dimensional) is a row, or a column of numbers. A more complex array (two- dimensional) is a collection of number arranged in rows and columns.

Matrix generation

Matrices are fundamental to MATLAB. Matrices can be generated in several ways.

Creating A One – Dimensional Array (Vector)

1. Row Vector

Enter a statement like

>> v = [1 4 7 10 13]

v =

1 4 7 10 13

2. COLUMN VECTORS

Column vectors are created in a similar way, however, semicolon (;) must separate the

Components of a column vector,

>> w = [1;4;7;10;13]

w =

1

4

7

10

13

Initializing vectors: the colon operator

A vector can also be generated (initialized) with the colon operator, Enter the following statements:

x = 1:10

(elements are the integers 1, 2, …, 10);

x = 1:0.5:4

(elements are the values 1, 1.5, …, 4 in increments of 0.5—note that if the colons separate three values, the middle value is the increment);

x = 10:-1:1

(elements are the integers 10, 9, …, 1, since the increment is negative);

x = 1:0

(a complicated way of generating an empty vector!).

Creating a vector with constant spacing

A vector in which the first element is xi, the last element is xf, and the number of elements is n is created by typing the linspace command (MATLAB determines the correct spacing):

variable_name = linspace(xi,xf,n)

>> b=linspace(3,13,6)

b =                    6 elements, first element 3, last element 13.

3 5 7 9 11 13

 
 

 

Monday 17 September 2012

Defining Scalar Variables

 

A variable is a name made of a letter or a combination of several letters (and digits) that is assigned a numerical value. Name of variables must begin with a letter. After the first letter, any number of digits or underscores may be used.

Rules About Variable Name
· Variable name can be up to 31 characters long.
· Can contain letters, digits, and the underscore character.
· Must begin with a letter.
· MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters. For example, FF, Ff, fF, and ff are the names of four different variables.
· Avoid using the names of a built-in function for a variable (i.e. avoid using: sin, tan, exp, sqrt, etc.)

Predefined Variables
A number of frequently used variables are already defined when MATLAB is started. Some of the predefined variables are:



Table : predefined Variables
Predefined Variables Description
ans A variable that has the last expression that was not assigned to a specific variable. If the user does not assign the value of an expression to a variable, MATLAB automatically stores the result in ans.
pi The π number, π = 3.14159
eps The smallest difference between two numbers. Equal to 2^(-52), which is approximately 2.2204e-016.
inf Used for infinity.
i Imaginary unit, defined as , which is 0+1.0000i.
j Same as i.
NaN Stands for Not a number. Used when MATLAB cannot determine a valid numeric value. For example 0/0.