Matlab - Tutor3 - Variables and Arrays
Matlab - Tutor3 - Variables and Arrays
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.
logical magic(4) > Array of logical value of 1 or 0 to represent true and false
10 respectively. Two-dimensional arrays can be sparse.
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.
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
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:
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
isa It determines if the input value has the specified data type.
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.
Bits Usage
Floating-Point Functions
Function Description
isa It determines if the input value has the specified data type.
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.
Function Description
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.
+ Addition plus
- Subtraction minus
PlayNext
Mute
Duration 18:10
Loaded: 2.94%
Fullscreen
ADVERTISEMENT
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
== Equal to eq
~= Not equal to ne
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.
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.
A = [0 1 1 0 1];
B = [1 1 0 0 1];
& 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
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
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.
@ 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
, 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
! Exclamation point
o Operating system command
? Question mark
o Metaclass for MATLAB class
~ 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.