CS 211 Lesson 20
Multidimensional Arrays and Sparse Arrays
Quote:
Let others lead small lives, but not you. Let others argue over small things, but not you. Let others cry over small hurts, but not you. Let others leave their future in someone else's hands, but not you. Jim Rohn
Lesson Objectives:
Be able to create and use multidimensional arrays
Understand when to use sparse matrices
Be able to create and use sparse matrices
Lesson:
I. MATLAB Concepts
A. Multidimensional Arrays
MATLAB supports arrays of more than 2 dimensions.
There is no limit to the number of dimensions. The number of elements in an array is limited by the available primary memory (RAM).
n-dimensional arrays may be created with array constructor functions with n arguments. For example:
zeros(2, 3, 4) % creates a 2x3x4 3-D matrix of all zeros
ones(2, 2, 2, 2) % creates a 2x2x2x2 4-D matrix of all ones
rand(4, 4, 4) % creates a 4x4x4 3-D matrix of random numbersMulti-dimensional arrays also may be created by directly assigning values to elements. For example:
M = [1 2; 3 4] % creates a 2x2 2-D matrix
M(:,:,2) = [5 6; 7 8] % adds 2nd layer making M a 2x2x2 3-D matrix
M(:,:,3) = [9 0; 1 2] % adds 3rd layer making M a 2x2x3 3-D matrixUse the ndims(Variable) function to get the number of dimensions of an array.
Use the size(Variable) function to get the length of all dimensions of a matrix. (Returns a vector.)
Use the size(Variable, Dimension) function to get the length of an individual dimension of a matrix. For example,
Number_rows = size(Array,1);
Number_columns = size(Array,2);
Number_slices = size(Array,3);Always use n indices to reference a single element within an n-dimensional array.
You can reference sub-arrays of an n-dimensional array. For example,
Cube = rand(3, 3, 3)
Center_layer = Cube(:, :, 2) % Every row and column value on slice 2
Corner_cube = Cube(1:2, 1:2, 1:2) % a 2x2x2 subarray of the 3x3x3 Cube arrayUse n nested for loops to access all elements of an n-dimensional array individually. For example,
Matrix = rand(2,3,4);
[Num_rows Num_cols Num_layers] = size(Matrix);
for Row = 1:Num_rows
for Col = 1:Num_cols
for Layer = 1:Num_layers
fprintf('Matrix(%d,%d,%d) = %g\n', Row, Col, Layer, ...
Matrix(Row, Col, Layer));
end
end
end
B. Sparse Matrices
A sparse matrix is any matrix that contains very few non-zero elements? Or said the other way, a sparse matrix is any matrix who's elements are mostly zeros. For example, the matrix below is sparse.
1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 4 0 0 0 0
0 0 0 0 0 0 3 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 2 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 6 0 0
0 0 0 5 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 1Sparse matrices occur often in science and engineering problems. In addition, real world problems often generate large sparse matrices.
MATLAB has a special data type for storing and manipulating sparse matrices because:
You can save memory by not storing all of the zero elements in a sparse matrix.
Vector and matrix operations can be sped up significantly by not processing the elements that are zero.
Therefore, using sparse matrices allows for the calculation of larger problem sizes (because of the memory and CPU time savings) as compared to using "normal" (full) arrays.
You can create a sparse matrix in MATLAB using the following commands
MATLAB function Description speye(n) % Create an identity matrix (all 1's down the diagonal) that has n rows and n columns, but store it as a sparse matrix. sprand(n,m,density) . % Create a sparse matrix that has n rows and m columns, with a specified density (a fraction between 0 and 1). For example, if the density is 0.1, approximately 10% of the elements in the matrix will be non-zero. sprandn(n,m,density) % Same as sprand(), except the non-zero elements are random in a normal distribution.
You can convert between a full matrix and a sparse matrix using the following commands
MATLAB function Description sparse() % Given a full matrix, return an equivalent sparse matrix. full() % Given a sparse matrix, return an equivalent full matrix.
When displaying a sparse matrix using the disp() function, only the non-zero elements and their locations are displayed.
You access and process sparse matrices in the same way that you access and process full matrices. (However, the internal MATLAB functions work differently on sparse matrices because of the way they are stored.) Some examples,
A = speye(100); % a sparse identity matrix
B = rand(100); % a full 100-by-100 element array
C = A * B; % works fine - produces a full matrix result
whos A B C % Note that C is not sparse
A = speye(100); % a sparse identity matrix
disp( A(2,6) );
disp( A(37,12) );
disp( A(5,5) );
A = sprand(100,100, 0.05); % a sparse matrix with only 5% non-zero values
B = sprand(100,100, 0.07); % a sparse matrix with only 7% non-zero values
C = A * B;
whos A B C % Note that C is sparse
II. Good Programming Practices
Use sparse arrays when your problem generates large matrices with many zero elements. It will save memory and potentially execute faster.
III. Algorithms
(None for this lesson)
Lab Work: Lab 20
References: Chapman Textbook: sections 6.3, 7.1