# Connexions

You are here: Home » Content » Basic operations in MATLAB

## Navigation

### Recently Viewed

This feature requires Javascript to be enabled.

# Basic operations in MATLAB

Based on: Using MATLAB by Anders Gjendemsjø

Summary: This module covers basic operations in MATLAB.

Note: You are viewing an old version of this document. The latest version is available here.

## Basic Operations on Numbers

MATLAB has many arithmetic operations and functions built in. Most of them are straightforward to use. The Table below lists some commonly used scalar operations; in this table, x and y are scalars. (A scalar is a single number.)

Table 1: Common scalar mathematical operations in MATLAB
Operation MATLAB
xyxy x-y
x+yxy x+y
xyxy x*y
xyxy x/y
xyxy x^y
exex exp(x)
log10xlog10x log10(x)
lnxlnx log(x)
log2xlog2x log2(x)

Expressions are formed from numbers, variables, and these operations. The operations have different precedences. The ^ operation has the highest precedence; ^ operations are evaluated before any other operations. Multiplication and division have the next highest precedence, and addition and subtraction have the lowest precedence. Precedence is altered by parentheses; expressions within parenthesesare evaluated before expressions outside parentheses.

### Example 1

The Table below shows several mathematical formulas, the corresponding MATLAB expressions, and the values that MATLAB would compute for the expressions.

Table 2: Example MATLAB Expressions
formula MATLAB Expression Computed Value
52+42 52 42 5^2+4^2 41
5+42 54 2 (5+4)^2 81
2+345 23 45 (2 + 3)/(4 - 5) -5
log10100 log10 100 log10(100) 2
ln4×(2+3) ln 4 23 log(4*(2+3)) 2.9957

## Basic Operations on Matrices

In addition to scalars, MATLAB can operate on matrices. Some common matrix operations are shown in the Table below; in this table, M and N are matrices.

Table 3: Common matrix mathematical operations in MATLAB
Operation MATLAB
MNMN M*N
M-1M-1 inv(M)
MTMT M'
det(MM) det(M)

MATLAB functions length and size are used to find the dimensions of vectors and matrices, respectively.

MATLAB can perform an operation on each element of a vector or matrix. To perform an arithmetic operation on each element in a vector (or matrix), rather than on the vector (matrix) itself, then the operator should be preceded by ".", e.g .*, .^ and ./.

### Example 2

Let A=( 1 1 1 1 ) A 1 1 1 1 . Then A^2 will return AA=( 2 2 2 2 ) AA 2 2 2 2 , while A.^2 will return ( 1212 1212 )=( 1 1 1 1 ) 12 12 12 12 1 1 1 1 .

### Example 3

Given a vector x, compute a vector y having elements yn=1sinxn yn 1 xn . This can be easily be done in MATLAB by typing y=1./sin(x) Note that using / in place of ./ would result in the (common) error Matrix dimensions must agree.

## Complex numbers

MATLAB has excellent support for complex numbers with several built-in functions available. The imaginary unit is denoted by i or (as preferred in electrical engineering) j. To create complex variables z1=7+i z1 7 and z2=2eiπ z2 2 e simply enter z1 = 7 + j and z2 = 2*exp(j*pi)

The Table below gives an overview of the basic functions for manipulating complex numbers, where zz is a complex number.

Table 4: Manipulating complex numbers in MATLAB
MATLAB
Re(zz) real(z)
Im(zz) imag(z)
|z|z abs(z)
Angle(zz) angle(z)
z*z* conj(z)

## Other Useful Details

• A semicolon added at the end of a line tells MATLAB to suppress the command output to the display.
• MATLAB Version 7 is case sensitive for both variables and functions; for example, b and B are different variables and MATLAB will recognize the built-in function sum but not SUM. In previous versions, MATLAB was not case sensitive for function names.
• Often it is useful to split a statement over multiple lines. To split a statement across multiple lines, enter three periods ... at the end of the line to indicate it continues on the next line.

### Example 4

Splitting y=a+b+c y a b c over multiple lines.


y = a...
+ b...
c;

## Content actions

### Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

#### Definition of a lens

##### Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

##### What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

##### Who can create a lens?

Any individual member, a community, or a respected organization.

##### What are tags?

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks