1. Numeric Arrays

Numeric Data Types in MATLAB

• single, double: single-precision floating-point values (32 bits) and double-precision floating-point values (64 bits)

• int8, int16, int32, int64: #-bit signed integers

• uint8, uint16, uint32, uint64: #-bit unsigned integers

• NaN, Inf

Fortran Ordering and Array Indexing

MATLAB uses Fortran (column-wise) ordering of data.

• It is contrary to C/C++ (row-wise), in which if we store a m*n two-dimensional matrix as a linear array, we should dimensionalize it by ourself, such as using i*m+j to indexing.

• First dimension is the fastest varying dimension, which means we first vary rows, then columns. For example, if we store 11, 12, …, 18 in a 2*2*2 three-dimensional array:

  >> M = reshape(11:18, [2,2,2])

M(:, :, 1) =
11    13
12    14

M(:, :, 2) =
15    17
16    18

• This Fortran ordering and linear storage allow us to index into array with single index or a vector of indices (this is how the array is really stored in memory). In above example, we can assume M is a vector of size 8, even though M is a three-dimensional array. For example:

  % linear indexing (scalar)
>> M(1)
ans =
11

>> M(8)
ans =
18

% linear indexing (vector)
>> M(5:8)
ans =
15    16    17    18

>> M([1, 3, 4, 8])
ans =
11    13    14    18


The vectors of indices we give determines the shape of the output. In above code, since we are giving linear vectors, our output is going to be a vector as well.

• If we have indices inputed as matrix, then this means we want our output to be a matrix as well (of same size as the index matrix):

  % linear indexing (matrix)
>> M([1, 5, 2; 8, 3, 2; 7, 4, 6])
ans =
11    15    12
18    13    12
17    14    16

• We can also access the elements using component form:

  % component indexing (matrix)
>> M([1, 2], [2, 1], [2, 1])

ans(:, :, 1) =
17    15
18    16

ans(:, :, 2) =
13    11
14    12


Component indexing is more common than linear because it is more straightforward, but the linear indexing option is always there since this is how the array is really stored in memory.

• Component-wise indexing with matrices is equivalent to component-wise indexing with vectorization of matrix (first varying row dimension, then column dimension):

  >> M([1, 2; 1, 2], [2, 1], 1)
ans =
13    11
13    11
14    12
14    12

>> M([1, 1, 2, 2], [2, 1], 1)
ans =
13    11
13    11
14    12
14    12


Reshaping Arrays

• reshape(X, [m, n, p, ...]): reshape X into size m*n*p*... (overall dimension must be the same as the original)

  >> N = rand(100,1);
>> size(reshape(N, [50, 2]))
ans =
50    2

>> size(reshape(N, [25, 2, 2]))
ans =
25    2    2

• repmat(X, [m, n, p, ...]): repeat X along dimensions m*n*p*... (stack the pattern vertically or horizontally)

  >> size(repmat(N, [4, 3]))
ans =
400    3

>> size(reshape(N, [4, 3, 2]))
ans =
400    3    2

• fliplr(X): flip matrix in left/right direction

• flipud(X): flip matrix in up/down direction

• squeeze(X): remove singleton dimensions (remove a dimension which is 1)

  >> A = [1, 2; 3, 4];
>> A(:, :, 2) = [5, 6; 7, 8];
>> size(A(:, :, 1))
ans =
2    2

% note the difference caused by Fortran ordering
>> size(A(1, :, :))
ans =
1    2    2

>> size(squeeze(A(1, :, :)))
ans =
2    2


2. Cells and Cell Arrays

Cell Array Creation and Indexing

• Cell array is very useful in certain applications when we need to store a variety of different data types.

• Cell array is a collection of data of any MATLAB type. It allows us to store not only numerical, but also characters, strings, etc.

• This flexibility sacrifices storage efficiency. The in-between cells within a cell array are not stored contiguously in memory. Each cell is contiguous, but not two cells next to each other.

• A cell array is constructed with {} or cell. Each cell array is a MATLAB array of cell, which can be mixed data types.

• Indexing

• Cell containers indexed using (): c(i) returns ith cell of cell array c. This cannot be used to modify elements. Instead, we may use t = c(i) and t{1} to access the content.

• Cell contents indexed using {}: c{i} returns contents of ith cell of cell array c

  % for loop in cell array
>> c = {14, [1, 2; 5, 6], 'hello world'};
>> for t = c, class(t), end
ans =
'cell'

ans =
'cell'

ans =
'cell'

>> for t = c, t{1}, end
ans =
14

ans =
1    2
5    6

ans =
'hello world'

• cellplot(c) provides a handy feature to help us visualize our data in cell array c.

• We know that create/access cell array entry requires using {}.

• To assign contents of container, we use = as usual.

• To delete cell from cell array, we should grab cell containers with () and then assign the value [] to it. Note: assigning elements with {} and set them to [] does not delete cell entries. It will only clear the data in that cell.

  >> A = {};
>> A{1, 1} = 'up left diagonal entry'; A{2, 2} = 'bottom right diagonal entry'
A =
'up left diagonal entry'                               []
[]    'bottom right diagonal entry'

>> A{1, 1} = []    % does not delete cell
A =
[]                               []
[]    'bottom right diagonal entry'

>> A(1, :) = []    % delete cells in the first row
A =
[]    'bottom right diagonal entry'


Memory Requirements

• Cell arrays require additional memory to store information describing each cell, which are stored in a header and label it as a double, character, string, etc. This means we have some memory required for header of each single cell.

  >> c = {[]}; s = whos('c'); s.bytes
ans =
112    % 112 bytes required just for the header, even it is empty

• Memory required for whole cell array: (head_size * number_of_cells) + data

• Contents of a single cell stored contiguously

• Storage not necessarily contiguous between cells in array

Comma-Separated Lists via Cell Arrays

• We can construct a list of MATLAB objects separated by commas and each item displayed individually when printed.

• This is useful in passing arguments to functions (e.g., plotting) and assigning output variables.

  >> plotStyle = {'bo−', 'linewidth', 2, 'markerfacecolor', 'r'};
>> plot(1:10, plotStyle{:})    % Pass comma−separated list to function