Numeric Arrays

  • Numeric classes in MATLAB include signed and unsigned integers, and single-precision and double-precision floating-point numbers.
  • By default, MATLAB stores all numeric values as double-precision floating point. You can choose to store any number, or array of numbers, as integers or as single-precision.
double Convert to double precision
single Convert to single precision
int8 Convert to 8-bit signed integer
int16 Convert to 16-bit signed integer
int32 Convert to 32-bit signed integer
int64 Convert to 64-bit signed integer
uint8 Convert to 8-bit unsigned integer
uint16 Convert to 16-bit unsigned integer
uint32 Convert to 32-bit unsigned integer
uint64 Convert to 64-bit unsigned integer
In [1]:
y = int64(-6548313425); % Create a 64-bit signed integer
x = double(y) % Convert to double precision floating number
x =

  -6.5483e+09


In [2]:
x = single(y) % Convert to single-precision floating number
x =

  single

 -6.5483e+09


In [3]:
mydata = uint16(magic(3)) % Convert a double array to uint16
% magic(3) is a double array by default
mydata =

  3×3 uint16 matrix

   8   1   6
   3   5   7
   4   9   2


In [4]:
double('a') % Convert a character to a double-precision format
% the decimal equivalent in the ASCII coding system
ans =

    97


Character Arrays

Chracter arrays provide storage for text data in MATLAB

  • Store text by enclosing a sequence of characters in single quotation marks
  • Store several separate pieces of text in one collection by using a cell array
  • char: Converts to character array
In [5]:
char(98)
% the character represented by that decimal number in ASCII
ans =

    'b'


  • Creating character array:

    • Create a character vector by enclosing a sequence of characters in single quotation marks.
      • chr = 'Hello' %1x5 character array
    • You can join two or more character vectors together to create a character array
In [6]:
title = ['Harold A. Jorgensen      ' ; 'Assistant Project Manager' ; 'SFTware Corp.            ']
% Each row must contain the same number of characters
title =

  3×25 char array

    'Harold A. Jorgensen      '
    'Assistant Project Manager'
    'SFTware Corp.            '


In [7]:
['a', 98]
ans =

    'ab'


In [8]:
name = 'myname' ;
domain = 'mydomain' ;
ext = 'com' ;
address = strcat(name , '@' , domain , '.' , ext )
address =

    'myname@mydomain.com'


In [9]:
Q = char('Holly','Steven','Meagan','David','Michael','Heidi')
Q =

  6×7 char array

    'Holly  '
    'Steven '
    'Meagan '
    'David  '
    'Michael'
    'Heidi  '


In [10]:
R = [98;84;73;88;95;100];
table = [Q,R]
table =

  6×8 char array

    'Holly  b'
    'Steven T'
    'Meagan I'
    'David  X'
    'Michael_'
    'Heidi  d'


Cell Arrays

  • A cell array is a data type with indexed data containers called cells, where each cell can contain any type of data. Cell arrays commonly contain either lists of text strings, combinations of text and numbers, or numeric arrays of different sizes.
  • Creating cell array:
In [11]:
A = 1:3;
B = ['abcdefg'] ;
C = single([1 2 3; 4 5 6]);
my_cellarray = {A, B, C}
my_cellarray =

  1×3 cell array

    [1×3 double]    'abcdefg'    [2×3 single]


In [12]:
my_cellarray{1}
ans =

     1     2     3


In [13]:
my_cellarray{2}
ans =

    'abcdefg'


In [14]:
my_cellarray{3}
ans =

  2×3 single matrix

     1     2     3
     4     5     6


In [15]:
my_cellarray{1}(1,2)
ans =

     2


In [16]:
my_cellarray{3}(2,3)
ans =

  single

     6


In [17]:
myCell = {1, 2, 3; 'abc', rand(5,10,2) , {11; 22 ; 33}}
myCell =

  2×3 cell array

    [  1]    [            2]    [       3]
    'abc'    [5×10×2 double]    {3×1 cell}


In [18]:
myCell(1:2,2:3)
ans =

  2×2 cell array

    [            2]    [       3]
    [5×10×2 double]    {3×1 cell}


  • cell(n): Returns an n-by-n cell array of empty matrices
In [19]:
cell(3)
ans =

  3×3 cell array

    []    []    []
    []    []    []
    []    []    []


  • cell(m,n): Returns an m-by-n cell array of empty matrices
In [20]:
cell(2,4)
ans =

  2×4 cell array

    []    []    []    []
    []    []    []    []


  • cell(s1,s2, ..., sN): Returns a s1-by-s2-by-...-by-sN cell array of empty matrices where s1, s2, ..., sN indicate the size of each dimension
In [21]:
A = cell(3,4,2,2)
  3×4×2×2 cell array

A(:,:,1,1) = 

    []    []    []    []
    []    []    []    []
    []    []    []    []


A(:,:,2,1) = 

    []    []    []    []
    []    []    []    []
    []    []    []    []


A(:,:,1,2) = 

    []    []    []    []
    []    []    []    []
    []    []    []    []


A(:,:,2,2) = 

    []    []    []    []
    []    []    []    []
    []    []    []    []


In [22]:
size(A)
ans =

     3     4     2     2


  • cell ( size (A)): Create a cell array of empty matrices that is the same size as matrix A
In [23]:
A = [1 3 4 ; 2 8 6];
cell(size(A))
ans =

  2×3 cell array

    []    []    []
    []    []    []


Structure Arrays

  • Structure arrays are similar to cell arrays. Multiple arrays of differing data types can be stored in structure arrays, just as they can in cell arrays. Instead of using content indexing, however, each matrix stored in a structure array is assigned a location called a field. Each field can contain any type of data.
  • Creating structure array:
In [24]:
A = 1:3;
B = ['abcdefg'];
C = single([1 2 3; 4 5 6]);

In [25]:
my_structure.numbers = A;
my_structure.numbers
ans =

     1     2     3


In [26]:
my_structure.letters = B;
my_structure.letters
ans =

    'abcdefg'


In [27]:
my_structure.othernumbers = C;
my_structure.othernumbers
ans =

  2×3 single matrix

     1     2     3
     4     5     6


In [28]:
my_structure
my_structure = 

  struct with fields:

         numbers: [1 2 3]
         letters: 'abcdefg'
    othernumbers: [2×3 single]


  • struct( field ,value): Creates a structure array with the specified field and values. The value input argument can be any data type, such as a numeric, logical, character, or cell array
In [29]:
f = 'myfield';
v = {'abc'; [1;2;3]; magic(5)};
struct(f,v)
ans = 

  3×1 struct array with fields:

    myfield


  • struct(field1 ,value1, ..., fieldN ,valueN): creates a structure array with multiple fields. Any nonscalar cell arrays in the set value1, ..., valueN must have the same dimensions
In [30]:
field1 = 'f1'; value1 = zeros(1,10);
field2 = 'f2' ; value2 = {'a' , 'b'};
field3 = 'f3' ; value3 = {pi , pi.^2};
field4 = 'f4' ; value4 = {'fourth'};
s = struct(field1, value1, field2, value2, field3, value3, field4, value4)
s = 

  1×2 struct array with fields:

    f1
    f2
    f3
    f4


In [31]:
s(2)
ans = 

  struct with fields:

    f1: [0 0 0 0 0 0 0 0 0 0]
    f2: 'b'
    f3: 9.8696
    f4: 'fourth'


In [32]:
s = struct('f1', 'a', 'f2', [ ]) % Creates a structure with an empty field
s = 

  struct with fields:

    f1: 'a'
    f2: []


In [33]:
s = struct('f1', {}, 'f2', {} , 'f3', {}) % Creates an empty structure with several fields
s = 

  0×0 empty struct array with fields:

    f1
    f2
    f3


In [34]:
s(1).f2 = ('b') % Assings a value to a field in an empty structure
s = 

  struct with fields:

    f1: []
    f2: 'b'
    f3: []


Multidimensional Arrays

  • An array having more than two dimensions is called a multidimensional array
  • Creating multidimensional array:
In [35]:
x = [ 1 2 3 ; 4 5 6];
y = 10*x;
z = 10*y;
w = 10*z;
my_3D_array(:,:,1) = x;
my_3D_array(:,:,2) = y;
my_3D_array(:,:,3) = z;
my_3D_array(:,:,4) = w;

In [36]:
my_3D_array
my_3D_array(:,:,1) =

     1     2     3
     4     5     6


my_3D_array(:,:,2) =

    10    20    30
    40    50    60


my_3D_array(:,:,3) =

   100   200   300
   400   500   600


my_3D_array(:,:,4) =

        1000        2000        3000
        4000        5000        6000


Data Type Identification

iscategorical Determine whether input is categorical array
iscell Determine whether input is cell array
ischar Determine whether input is character array
isfloat Determine whether input is floating-point array
isinteger Determine whether input is integer array
islogical Determine whether input is logical array
isnumeric Determine whether input is numeric array
isstruct Determine whether input is structure array
class Determine the class of the object
In [37]:
myCell = {1, 2, 3; 'abc', rand(5,10,2) , {11; 22 ; 33}}
myCell =

  2×3 cell array

    [  1]    [            2]    [       3]
    'abc'    [5×10×2 double]    {3×1 cell}


In [38]:
class(myCell)
ans =

    'cell'


In [39]:
iscell(myCell)
ans =

  logical

   1


In [40]:
ischar(myCell)
ans =

  logical

   0


Data Type Conversion

cellstr Convert to cell array of character vectors
int2str Convert integer to string
mat2str Convert matrix to string
num2str Convert number to string
str2double Convert string to double-precision value
str2num Convert string to number
cell2struct Convert cell array to structure array
mat2cell Convert array to cell array with potentially different sized cells
num2cell Convert array to cell array with consistently sized cells
struct2cell Convert structure to cell array
In [41]:
a = magic(3)
a =

     8     1     6
     3     5     7
     4     9     2


In [42]:
num2cell(a)
ans =

  3×3 cell array

    [8]    [1]    [6]
    [3]    [5]    [7]
    [4]    [9]    [2]