0% found this document useful (0 votes)
6 views

Matlab - Tutor3 - Variables and Arrays

Uploaded by

Gemini Goel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Matlab - Tutor3 - Variables and Arrays

Uploaded by

Gemini Goel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

MATLAB Data Types

The basic data type (also called a class) in MATLAB is the array or matrix.
There are 15 fundamental data types in MATLAB. Each of these data types is
in the build of a matrix or array. This matrix or arrays are a minimum of 0-by-
0 in size and can increase to an n-dimensional array of any size.

The following table describes these data types are:

Data Example Description


Type

int8, uint16(65000 The array of signed and unsigned integers. It


uint8, ) requires less storage space than single or double.
int16, All integer data types except for int64 and uint64
uint16, can be used in mathematical operations.
int32,
uint32,
int64,
uint64

single 3 * 10^38 The array of single-precision numbers. It requires less storage


space than double but has less precision and a smaller range.

double 3 * 10^300 Array of double-precision numbers. Two-dimensional array can


5 + 6i be sparse. The default numeric types in MATLAB.

logical magic(4) > Array of logical value of 1 or 0 to represent true and false
10 respectively. Two-dimensional arrays can be sparse.

char 'Hello' Array of characters. Strings are represented as vectors of


characters. For arrays including more than one string, it is good
to use cell arrays.

cell array a{1,1} = 12; Array of indexed cells, each capable of saving an array of a
a{1,2} = various dimension and data type.
'Red';
a{1,3} =
magic(4);

structure a.day = 12; Array of C-like structures, each structure having named fields
a.color = capable of storing an array of a different dimension and data
'Red'; type.
a.mat =
magic(3);

function @sin Pointer to a function. You can pass function handles to other
handle functions.

user polynom([0 - Objects constructed from a user-defined class.


class 2 -5])

Java java.awt.Fra Objects constructed from a Java class.


class me

Numeric Types
Numeric data types in MATLAB contain signed and unsigned integers, and
single- and double-precision floating-point numbers. Integer and single-
precision array offer more memory efficient storage than double-precision.
All numeric types provide basic array functions, such as subscripting and
reshaping. All numeric types except for int64 and uint64 can be used in
numerical operations.

PauseNext
Mute

Current Time 0:00

Duration 18:10
Loaded: 2.94%
Â
Fullscreen

Integers
MATLAB has four signed and four unsigned integers data type.

Signed types enable to work with a negative integer as well as positive, but
cannot perform as wide a range of number as the unsigned type because
one bit is used to designate positive or negative signs for the number.

Unsigned types give a wider range of numbers, but these numbers can only
be zero or positive.

MATLAB provides 1-, 2-, 4-, and 8-byte storage for numeric data. We can
save memory and execution time for our programs if we use the smallest
integer type that accommodates your data. For example, we don't need a
32-bit integer to save the value 100.

Here are the eight numeric data type, the range of values can save with
each type, and the MATLAB conversion operation required to create that
type:

Data Type Range of Values Conversion


Function

Signed 8-bit integer -27 to 27-1 int8

Signed 16-bit integer -215 to 215-1 int16


Signed 32-bit integer -231 to 231-1 int32

Signed 64-bit integer -263 to 263-1 int64

Unsigned 8-bit integer 0 to 28-1 uint8

Unsigned 16-bit integer 0 to 216-1 uint16

Unsigned 32-bit integer 0 to 232-1 uint32

Unsigned 64-bit integer 0 to 264-1 uint64

Creating Integer Data


MATLAB save numeric data as a double-precision floating-point by default. To
save data as an integer, use one of the conversion functions shown in the
table above:

1. x = int16 (32501);

We can use the whos function to show the dimensions, byte count, and data
type of an array represented by a variable:

1. whos x
2. Name Size Bytes Class
3. x 1x1 2 int16 array

We can use the class function if we want to assign the output as shown here:

1. xType = class(x)
2. xType =
3. int16

Use the isinteger function if you just want to verify that x is an integer:

1. isinteger(x)
2. ans =
3. 1

Integer Functions
Function Description

int8, int16, It convert to signed 1-, 2-, 4-, or 8-byte integer.


int32, int64

uint8, uint16, It converts to unsigned 1-, 2-, 4-, or 8-byte integer.


uint32, uint64

class It returns the data type of an object.

isa It determines if the input value has the specified data type.

isinteger It determines if input value is an integer array.

isnumeric It determines if the input value is a numeric array.

Floating-Point Numbers
MATLAB show floating-point numbers in either double-precision or single-
precision format. The default is double-precision, but we can make any
number of single-precision with a simple conversion function.

Double-Precision Floating Point


MATLAB composes the double data type according to IEEE Standard 754 for
double precision. Any value stored as a double-needed 64 bits, formatted as
shown in the table below:

Bits Usage

63 Sign (0 = positive, 1 = negative)

62 to 52 Exponent, biased by 1023

51 to 0 Fraction f of the number 1.f

Single-Precision Floating Point


MATLAB composes the single data type according to IEEE Standard 754 for
single precision. Any value save as a single require 32 bits, formatted as
shown in the table following:
Bits Usage

31 Sign (0 = positive, 1 = negative)

30 to 23 Exponent, biased by 127

22 to 0 Fraction f of the number 1.f

Floating-Point Functions

Function Description

double It converts to double precision.

single It converts to single precision.

class It returns the data type of an object.

isa It determines if the input value has the specified data type.

isfloat It determines if the input value is a floating-point array.

isnumeri It determines if the input value is a numeric array


c

eps It returns the floating-point relative accuracy. This value is the tolerance
MATLAB uses in its evaluation.

realmax It returns the largest floating-point number your computer can represent.

realmin It returns the smallest floating-point number our computer can represent.

Complex Numbers
Complex numbers consist of two separate part: a real part and an imaginary
part. The primary imaginary unit is equal to the square root of -1. This is
display in MATLAB by either of two letters: i or j.

Creating Complex Numbers


The following statement shows one method of creating a complex value in
MATLAB. The variable x is assigned a complex number with a real part of 2
and an imaginary part of 3:
x = 2 + 3i;

Complex Number Functions

Function Description

complex It construct complex data from real and imaginary components.

i or j It returns the imaginary unit used in constructing complex data.

real It returns the real part of a complex number

imag It returns the imaginary part of a complex number.

isreal It determines if a number is real or imaginary.

MATLAB Operator
An operator is a symbol that tells the compiler to perform various numerical
or logical manipulations. MATLAB is designed to operate mainly on whole
matrices and arrays. Therefore, functions in MATLAB work both on scalar and
non-scalar data.

MATLAB has several types of operators, symbols, and special characters to


deal with variables, functions, and arithmetic operations.

MATLAB Arithmetic Operators


Arithmetic operators help in performing simple arithmetic operations like
addition, subtraction, multiplication, division, and power.

Symbol Role Corresponding function

+ Addition plus

+ Unary plus uplus

- Subtraction minus

- Unary minus uminus


.* Element-wise multiplication times

* Matrix multiplication mtimes

./ Element-wise right division rdivide

.\ Element-wise left division ldivide

/ Matrix right division mrdivide

\ Matrix left division mldivide

.^ Element-wise power power

^ Matrix power mpower

.' Transpose transpose

' Complex conjugate transpose ctranspose

Arithmetic Operators and Arrays


Except for some matrix operators, MATLAB arithmetic operators work on
corresponding functions of arrays with equal dimensions. For vectors and
rectangular array, both operands must be the equivalent size unless one is a
scalar. If one operand is a scalar and the other is not, MATLAB applies the
scalar to every item of the other operand, this property is called scalar
expansion.

PlayNext

Mute

Current Time 0:00


/

Duration 18:10

Loaded: 2.94%

Fullscreen

ADVERTISEMENT

This example uses scalar expansion to evaluate the product of a scalar


operand and a matrix.

1. A = magic (3)
2. A =
3. 8 1 6
4. 3 5 7
5. 4 9 2
6. 3 * A
7. ans=
8. 24 3 18
9. 9 15 21
10. 12 27 6

MATLAB Relational Operators


Relational operators perform value comparison operations.

Symbol Role Corresponding function

== Equal to eq

~= Not equal to ne

> Greater than gt


>= Greater than or equal to ge

< Less than lt

<= Less than or equal to le

Relational Operators and Arrays


The MATLAB relational operators compare corresponding components of
arrays with equal dimensions. Relational operators always operate element-
by-element. In this example, the resulting matrix present where the element
of A is equal to the corresponding part of B.

1. A = [2 7 6; 9 0 5; 3 0.5 6];
2. B = [8 7 0; 3 2 5; 4 -1 7];
3. A == B
4. ans =
5. 0 1 0
6. 0 0 1
7. 0 0 0

For vectors and rectangular array, both operands must be the same size
unless one is a scalar. In this case, where one operand is a scalar, and the
other is not, MATLAB tests the scalar against every element of the other
operand. Locations where the particular relation is true receive logical 1.
Locations where the relation is false receive logical 0.

MATLAB Logical Operators


Logical operators perform logical operations and output the result in Boolean
state true or false using the numbers 1 and 0, respectively.

MATLAB offer three types of logical operators and functions:

ADVERTISEMENT

ADVERTISEMENT
o Element-wise: It works on corresponding elements of logical arrays.
o Bit-wise: It works on corresponding bits of integer values or arrays.
o Short-circuit: It works on scalar, logical expressions.

The values returned by MATLAB logical operators and functions, with the
exception of bit-wise functions, are of type logical and are suitable for use
with logical indexing.

Element-Wise Operators and Functions


The following logical operators and functions execute element-wise logical
operations on their inputs to produce a like-sized output array. The examples
are shown in the following table use vector inputs A and B, where

A = [0 1 1 0 1];
B = [1 1 0 0 1];

Symbo Role Descrip E


l tion x
a
m
p
l
e

& Logical AND It returns 1 for every element location that is true A & B =
(nonzero) in both arrays and 0 for all other elements. 01001

| Logical OR It returns 1 for every element location that is true (nonzero) A | B =


in either one or the other, or both arrays, and 0 for all other elements. 11101

~ Logical NOT It complements each element of the input array, A. ~A =


10010

xor It returns 1 for every element location that is true (nonzero) in only one xor(A,B)
array, and 0 for all other elements. =10100
For operators and functions that take two array operands (&, |, and xor),
both arrays must have the same dimensions, with each dimension being the
same size. The one exception to this is where one operand is a scalar, and
the other is not.

Note: MATLAB converts any finite nonzero, mathematic values used as inputs to logical
expressions to logical 1, or true.

Bit-Wise Operator
The following functions execute bit-wise logical operations on nonnegative
integer inputs. Inputs may be scalar or in arrays. If in arrays, these
operations produce a like-sized output array.

The examples are present in the following table use scalar inputs A and B,
where

A = 28; % binary 11100


B = 21; % binary 10101

Short-Circuit Operators
The following operators execute AND and OR operations on logical
expressions, including scalar values. They are short-circuiting operators in
that they calculate their second operand only when the first operand does
not fully determine the output.

Operator Description

&& It returns logical 1 (true) if both inputs calculate to true, and logical 0 (false) if
they do not.

|| It returns logical 1 (true) if either input, or both, calculate to true, and logical 0
(false) if they do not.

MATLAB Special Characters


Special characters perform some particular tasks according to their behavior
and the position where they are used.

Symbol Symbol Name Role

@ At symbol
o Function manage construction and reference
o Call super-class methods

. Period or dot
o Decimal point
o Element-wise operations
o Structure field access
o Object property or method specifier

... Dot dot dot or ellipsis


o Line continuation

, Comma
o Separator

: Colon
o Vector creation
o Indexing
o For-loop iteration

; Semicolon
o Signify end of the row
o Suppress output of code line

() Parentheses
o Operator precedence
o Function argument enclosure
o Indexing
[] Square brackets
o Array concatenation
o Array construction
o Empty matrix and array element deletion
o Multiple output argument assignment

{} Curly brackets
o Cell array assignment and contents

% Percent
o Comment
o Conversion specifier

%{ %} Percent curly bracket


o Block comments

! Exclamation point
o Operating system command

? Question mark
o Metaclass for MATLAB class

'' Single quotes


o Character array constructor

"" Double quotes


o String constructor

N/A Space character


o Separator

~ Tilde
o Logical NOT
o Argument placeholder

= Equal sign
o assignment
MATLAB String and Character Formatting Special
Characters
There are some special characters to use only within the text of a character
or string. These special characters are used to insert newlines or carriage
returns, specify folder paths.

Symbol Symbol Role Example


Name

/ Forward- File or folder path Windows:


\ slash separation dir([matlabroot '\toolbox\matlab\elmat\
Backslash scriptview1.m']) or
dir([matlabroot
'/toolbox/matlab/elmat/scriptview1.m'])
UNIX/Linux system: only forward slash
dir([matlabroot
'/toolbox/matlab/elmat/scriptview1.m'])

.. Dot dot Parent folder cd ..\..\example


Goes up two levels and then down into
the example folder

* Asterisk Wildcard character dir('example_*.mat')


Finds all files with names start with
example and have a .mat extension

@ At symbol Class folder indicator \@myScriptClass\get.m

+ Plus Package directory +mypack


indicator +mypack/scriptview1.m
+mypack/@myScriptClass

You might also like