Professional Documents
Culture Documents
Arrays
By : Dr. Atul R. Phadke
Associate Professor in Electrical Engineering
College of Engineering Pune (Maharashtra)
OBJECTIVES:
After studying this unit, you will be able to understand:
✓ How to create and manipulate MATLAB’s most common variable
types?
2
MATLAB VARIABLES:
It is useful to store the information in variables.
The assignment operator, denoted by the “=” symbol, is the operator
that is used to assign values to variables.
For example, >> x = 1 assigns the value 1 to the variable with name
“x”.
After executing this line, you will see a new variable “x” appear in the
workspace window.
Until the value is changed, or the variable deleted, the character x
behaves like the value 1.
Try it: Assign the value 5 to the variable y. Multiply y by 3.
Try it: Assign p = 2 and compute p = p + 3.
Note: You can view a list of all the variables in the command
window’s workspace using the function whos. 3
RESTRICTIONS ON VARIABLE NAMES:
Variables can only contain alphanumeric characters (letters and
numbers) as well as underscores.
However, the first character of a variable name must be a letter.
The maximum length of a variable name is 255 characters.
Spaces within a variable name are not permitted.
The variable names are case-sensitive (e.g., x and X will be
considered different variables).
Note: While programming, it is desirable to use variable name
associated with what they represent.
For example, to represent number of students, instead of using x, it is
better to use the variable name as no_of_stud
4
RESTRICTIONS ON VARIABLE NAMES:
Note: You can overwrite variables or functions that have been stored
in MATLAB.
For example, the command » factorial = 2 will store the value 2 in the
variable with name factorial.
After this assignment factorial will behave like the value 2 instead of
the function factorial.
command » pi = 2 will store the value 2 in the variable with name pi.
Therefore, you should not give your variables the same name as
built-in functions or values.
You can clear a variable from the workspace using the clear function.
» clear x will clear the variable x from the workspace.
Typing » clear or » clear all will remove all the variables from the
workspace. 5
CLEAR VARIABLES:
You can clear a variable from the workspace using the clear function.
» clear x will clear the variable x from the workspace.
Typing » clear or » clear all will remove all the variables from the
workspace.
Typing » clc will clear the screen but will not remove any of your
variables.
Try it: Execute the following lines and observe
>> x = 3;
>> y = 5;
>> clear x
>> x
Note: Semicolon suppresses output when it is at the end of line.
6
ARRAYS:
In MATLAB, every value is considered to be a matrix.
8
LENGTH OF AN ARRAY:
The length function returns the length of the largest dimension of matrix.
1 2 3 4
For example, if P = 5 6 7 8
9 10 11 12
length(p) will return [4]
If the matrix is one dimensional, the length function returns the number of
elements in the matrix.
1
For example, if x = [1 2 3] or 𝑥 = 2
3
Length(x) will return [4]
Note: Use length function for one dimensional array only. Do not use this function if
the matrix has more than one dimension.
9
COLON OPERATOR FOR GENERATION OF ARRAYS:
Let 𝑀 = 1 2 3 ⋯ 100
Using colon operator 𝑀 can be created with >> 𝑀 = 1: 1: 100
Colon operator:
10
FUNCTIONS ZEROS AND ONES:
Input to these functions is number of rows and number of columns and it
returns a matrix of zeros and ones with specified dimensions.
11
ARRAY INDEXING:
If A is an array in the current workspace, we can obtain the element in row r
and column c using the notation A(r, c).
1 2 3 4
For example, if P = 5 6 7 8
9 10 11 12
>> a = P(2, 3) will return a = 7
Try it: Let A = [1 2 3 4 5 6]. Write commands that retrieve the second
element of A, retrieve the third, fifth, and sixth elements of A, and
retrieve the third, fourth, and fifth elements of A.
Try it: Reassign the fourth element of A to 7. Reassign the first,
second, and third elements to 1.
12
ARRAY INDEXING:
If you want entire row or column, you can use colon operator.
1 2 3 4
For example, if P = 5 6 7 8
9 10 11 12
>> a = P(2, :) will return a = 5 6 7 8
Try it: Retrieve second, third and fourth element of second row of P.
Try it: Retrieve all elements of fourth column of P.
Try it: Reassign P(3, 4) to 100.
13
OPERATIONS BETWEEN A SCALAR (SINGLE NUMBER) AND AN ARRAY:
Let 𝑎 be a scalar and 𝑀 be a matrix.
14
OPERATIONS BETWEEN TWO ARRAYS:
Let, 𝑀 and 𝑃 are two matrices of same size.
15
MULTIPLICATION OF ARRAYS:
There are two different kinds of matrix multiplication (and division).
If 𝑀 and 𝑃 are the matrices of same size, then 𝑀 .* 𝑃 takes every element of 𝑀
and multiplies it by the corresponding element of 𝑃.
16
TRANSPOSE OF A MATRIX:
You can transpose a matrix in MATLAB using an apostrophe, ’.
1 2 3 4
For example, 𝑀 = 5 6 7 8
9 10 11 12
1 5 9
𝑃 = 𝑀′ returnes 𝑃 = 2 6 10
3 7 11
4 8 12
1 2 3 4
Try it: If 𝑀 = and 𝑃 = , compute the transpose of M and P
3 4 5 6
17
ARITHMETIC FUNCTIONS AND ARRAYS:
All built-in arithmetic like sqrt, factorial, sin, cos etc. can take array as input.
18
LOGICAL OPERATIONS AND ARRAYS:
Logical operations between two arrays can be performed if they are of same size.
19