0% found this document useful (0 votes)
46 views14 pages

Lab Session 02: Defining Scalar Variables

A variable is a name assigned to a memory location that stores a numerical value. In MATLAB, variables can be scalars, vectors, or matrices. Scalars have one element, vectors have elements arranged in a row or column, and matrices have elements arranged in rows and columns. Variables are defined using the assignment operator (=) and can later be reassigned new values or transposed. Arrays allow storing and manipulating multiple data values and their elements can be individually addressed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views14 pages

Lab Session 02: Defining Scalar Variables

A variable is a name assigned to a memory location that stores a numerical value. In MATLAB, variables can be scalars, vectors, or matrices. Scalars have one element, vectors have elements arranged in a row or column, and matrices have elements arranged in rows and columns. Variables are defined using the assignment operator (=) and can later be reassigned new values or transposed. Arrays allow storing and manipulating multiple data values and their elements can be individually addressed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Lab Session 02

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. Once a variable is assigned a numerical value, it can be used in mathematical
expressions, in functions, and in any MATLAB statements and commands. A variable is actually
a name of a memory location. When a new variable is defined, MATLAB allocates an appropriate
memory space where the variable’s assignment is stored. When the variable is used the stored data
is used. If the variable is assigned a new value the content of the memory location is replaced.
Assignment Operator

In MATLAB the = sign is called the assignment operator. The assignment operator assigns a value
to a variable.

The left-hand side of the assignment operator can include only one variable name. The right-hand
side can be a number, or a computable expression that can include numbers and/or variables that
were previously assigned numerical values. When the Enter key is pressed the numerical value of
the right-hand side is assigned to the variable, and MATLAB displays the variable and its assigned
value in the next two lines.

1
Several assignments can be typed in the same line. The assignments must be separated with a
comma (spaces can be added after the comma). When the Enter key is pressed, the assignments
are executed from left to right and the variables and their assignments are displayed. A variable is
not displayed if a semicolon is typed instead of a comma. For example, the assignments of the
variables a, B, and C above can all be done in the same line.

A variable that already exists can be reassigned a new value. For example:

Once a variable is defined it can be used as an argument in functions. For example:

Rules about Variable Names

A variable can be named according to the following rules:

 Must begin with a letter.


 Can be up to 63 characters long.

2
 Can contain letters, digits, and the underscore character.
 Cannot contain punctuation characters (e.g., period, comma, semicolon).
 MATLAB is case sensitive: it distinguishes between uppercase and lowercase letters. For
example, AA, Aa, aA, and aa are the names of four different variables.
 No spaces are allowed between characters (use the underscore where a space is desired).
 Avoid using the name of a built-in function for a variable (i.e., avoid using cos, sin, exp,
sqrt, etc.). Once a function name is used to define a variable, the function cannot be used.

Commands for Managing Variables

The following are commands that can be used to eliminate variables or to obtain information about
variables that have been created. When these commands are typed in the Command Window and
the Enter key is pressed, either they provide information, or they perform a task as specified
below;

Creating Arrays

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 numbers
arranged in rows and columns. One use of arrays is to store information and data, as in a table. In
science and engineering, one-dimensional arrays frequently represent vectors, and two-
dimensional arrays often represent matrices.

3
Creating One Dimensional Arrays (Vector)

A one-dimensional array is a list of numbers arranged in a row or a column. In MATLAB, a vector


is created by assigning the elements of the vector to a variable. This can be done in several ways
depending on the source of the information that is used for the elements of the vector. When a
vector contains specific numbers that are known (like the coordinates of point A), the value of each
element is entered directly. Each element can also be a mathematical expression that can include
predefined variables, numbers, and functions. Often, the elements of a row vector are a series of
numbers with constant spacing. In such cases the vector can be created with MATLAB commands.

i. Row Vector

To create a row vector type the elements with a space or a comma between the elements
inside the square brackets.

ii. Column Vector

To create a column vector type the left square bracket and then enter the elements with a
semicolon between them, or press the Enter key after each element. Type the right square
bracket after the last element.

4
iii. Creating a vector with constant spacing by specifying the first term, the spacing, and
the last term

In a vector with constant spacing the difference between the elements is the same. A vector
in which the first term is m, the spacing is q, and the last term is n is created by typing:

iv. Creating a vector with linear (equal) spacing by specifying the first and last terms,
and the number of terms

A vector with n elements that are linearly (equally) spaced in which the first element is xi
and the last element is xf can be created by typing the linspace command (MATLAB
determines the correct spacing):

5
Creating a Two-Dimensional Array (Matrix)

A two-dimensional array, also called a matrix, has numbers in rows and columns. Matrices can be
used to store information like the arrangement in a table. Matrices play an important role in linear
algebra and are used in science and engineering to describe many physical quantities.

A matrix is created by assigning the elements of the matrix to a variable. This is done by typing
the elements, row by row, inside square brackets [ ]. First type the left bracket then type the first
row, separating the elements with spaces or commas. To type the next row type a semicolon or
press Enter. Type the right bracket at the end of the last row.

The elements that are entered can be numbers or mathematical expressions that may include
numbers, predefined variables, and functions. All the rows must have the same number of elements.
If an element is zero, it has to be entered as such. MATLAB displays an error message if an attempt
is made to define an incomplete matrix.

Rows of a matrix can also be entered as vectors using the notation for creating vectors with
constant spacing, or the linspace command. For example:

6
The zeros, ones and, eye Commands

The zeros(m,n), ones(m,n), and eye(n) commands can be used to create matrices that have elements with
special values. The zeros(m,n) and the ones(m,n) commands create a matrix with m rows and n columns in
which all elements are the numbers 0 and 1, respectively. The eye(n) command creates a square matrix with
n rows and n columns in which the diagonal elements are equal to 1 and the rest of the elements are 0. This
matrix is called the identity matrix.

Variables in Matlab
 All variables in MATLAB are arrays. A scalar is an array with one element, a vector is an
array with one row or one column of elements, and a matrix is an array with elements in
rows and columns.
 The variable (scalar, vector, or matrix) is defined by the input when the variable is assigned.
There is no need to define the size of the array (single element for a scalar, a row or a
column of elements for a vector, or a two-dimensional array of elements for a matrix)
before the elements are assigned.

7
 Once a variable exists—as a scalar, vector, or matrix—it can be changed to any other size,
or type, of variable. For example, a scalar can be changed to a vector or a matrix; a vector
can be changed to a scalar, a vector of different length, or a matrix; and a matrix can be
changed to have a different size, or be reduced to a vector or a scalar. These changes are
made by adding or deleting elements.

Transpose Operator

The transpose operator, when applied to a vector, switches a row (column) vector to a column
(row) vector. When applied to a matrix, it switches the rows (columns) to columns (rows). The
transpose operator is applied by typing a single quote ’ following the variable to be transposed.

Array Addressing

Elements in an array (either vector or matrix) can be addressed individually or in subgroups. This
is useful when there is a need to redefine only some of the elements, when specific elements are
to be used in calculations, or when a subgroup of the elements is used to define a new variable.

i. Vector

The address of an element in a vector is its position in the row (or column). For a vector
named ve, ve(k) refers to the element in position k.

8
ii. Matrix

The address of an element in a matrix is its position, defined by the row number and the
column number where it is located. For a matrix assigned to a variable ma, ma(k,p) refers
to the element in row k and column p.

9
As with vectors, it is possible to change the value of just one element of a matrix by assigning a
new value to that element. Also, single elements can be used like variables in mathematical
expressions and functions.

Using a Colon (:) in Addressing Arrays

A colon can be used to address a range of elements in a vector or a matrix.

i. For a Vector

va(:) Refers to all the elements of the vector va (either a row or a column vector).
va(m:n) Refers to elements m through n of the vector va.

ii. For a Matrix

A(:,n) Refers to the elements in all the rows of column n of the matrix A.
A(n,:) Refers to the elements in all the columns of row n of the matrix A.
A(:,m:n) Refers to the elements in all the rows between columns m and n of the matrix A.
A(m:n,:) Refers to the elements in all the columns between rows m and n of the matrix A.
A(m:n,p:q) Refers to the elements in rows m through n and columns p through q of the
matrix A.

10
11
Practice Exercise

Solve the following problems by writing commands in the Command Window

3
1. Create a row vector that has the following elements: 3, 4.25, 68/16, 45, √110, cos 250,
0.05

(14𝑡𝑎𝑛480 )
2. Create a column vector that has the following elements: 25.5, , 6!, 2.74, 0.0375,
(2.12 +11)

π/5

32
3. Create a column vector that has the following elements: 3.22 , sin2350, 6.4, ln392, 0.00552,

ln239, 133

12
4. Define the variables x=0.85, y=12.54, and then use them to create a column vector that
has the following elements: y, yx, ln(y/x), y.x and x+y.

5. Create a row vector in which the first element is 2 and the last element is 37, with an
increment of 5 between the elements

6. Create a row vector with 9 equally spaced elements in which the first element is 81 and
the last element is 12.

13
7. Create a column vector in which the first element is 22.5, the elements decrease with
increments of –2.5, and the last element is 0.

8. Using the colon symbol, create a row vector (assign it to a variable named same) with seven
elements that are all –3

9. Use a single command to create a row vector (assign it to a variable named a) with 9 elements such
that the last element is 7.5 and the rest of the elements are 0s. Do not type the vector explicitly.

14

You might also like