You are on page 1of 20

MATLAB Variables and

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.

Even a single number is considered a 1 × 1 matrix.

You can build up arrays in MATLAB using square brackets, [ ].

Columns are separated by commas and rows are separated by


semicolons.

>> x = [1 2 3] or x = [1, 2, 3] will create matrix x = [1 2 3]


1
>> x = [1; 2; 3] will create matrix 𝑥 = 2
3
1 2 3
Try it: Create an array, P = 4 5 6
7 8 9
Note: Here semicolon is used to separate rows and not for
suppressing the output. 7
SIZE OF AN ARRAY:
The size function returns a 1x2 matrix (array) where the first element is the
number of rows in the matrix and the second element is the number of
columns in the matrix.
1 2 3 4
For example, if P = 5 6 7 8
9 10 11 12
size(p) will return [3 4]
1 2 3 4
Try it: Create an array, P = 5 6 7 8
9 10 11 12
>> s = size(P)

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:

start value : increment : final value

Note: If an increment is not specified, MATLAB uses a default value


of 1. for example, >> Z = 1:5 is same as >> Z = 1:1:5
Try it: Create an array, X = 0 0.2 0.4 0.6 0.8 1
Try it: Create an array, Z = 2 1.5 1 0.5 0

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.

For example, >> zeros(3,5) generates 3x5 array of zeros

>> ones(3,5) generates a 3x5 array of ones

Try it: Create a 4x7 array of ones and zeros.


Try it: Create a 4x7 array with each element = 5.

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

>> a = P(:, 3) will return a = 9 10 11 12

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.

>> 𝑀 + 𝑎 adds 𝑎 to every element of 𝑀

>> 𝑀 − 𝑎 subtracts 𝑎 from every element of 𝑀

>> 𝑀 ∗ 𝑎 multiplies every element of 𝑀 by 𝑎

>> 𝑀/𝑎 divides every element of 𝑀 by 𝑎


1 2 3 4
Try it: If P = 5 6 7 8 . Add and subtract 3 from P. Multiply
9 10 11 12
and divide P by 3.

14
OPERATIONS BETWEEN TWO ARRAYS:
Let, 𝑀 and 𝑃 are two matrices of same size.

>> 𝑀 − 𝑃 subtracts every element of 𝑃 from the corresponding element of 𝑀.

>> 𝑀 + 𝑃 adds every element of 𝑀 to the corresponding element of 𝑃.


1 2 7 8
Try it: If 𝑀 = 3 4 and 𝑃 = 9 10 , compute 𝑀 + 𝑃 and 𝑀 − 𝑃.
5 6 11 12

15
MULTIPLICATION OF ARRAYS:
There are two different kinds of matrix multiplication (and division).

Standard matrix multiplication and element by element matrix multiplication.

* for standard matrix multiplication.

.* for element by element matrix multiplication.

If 𝑀 and 𝑃 are the matrices of same size, then 𝑀 .* 𝑃 takes every element of 𝑀
and multiplies it by the corresponding element of 𝑃.

Same is true for ./ and .^


1 2 3 4
Try it: If 𝑀 = and 𝑃 = , compute 𝑀 ∙∗ 𝑃, 𝑀 ∙/𝑃 and 𝑀 ∙ ^𝑃.
3 4 5 6

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.

For example 𝑥 = 4 9 16 , sqrt(x) will return 2 3 4

Try it: If x = 1 2 3 4 5 6 , compute factorial (x), and exp(x)

18
LOGICAL OPERATIONS AND ARRAYS:
Logical operations between two arrays can be performed if they are of same size.

Logical operation between two arrays is conducted element by element.

Try it: If x = 1 2 3 4 5 6 and y = 6 5 4 3 2 1 , check


which elements of x are greater than the corresponding elements of y.
Logical operation between a scalar and an array is conducted between the
scalar and each element of array.
Try it: If x = 1 9 3 10 4 6 , check which elements of x are greater
than 3.
Try it: If x = 1 9 3 10 4 6 , create a variable m which contains all
the elements of x greater than 4.
>> m = x(x > 4)

19

You might also like