Lab: Advanced Data Structures
Advanced Data Structures
Sparse Matrix
A matrix representing connections between nodes in a graph can have many zeros.
For example, a matrix can have 81 elements where only 26 are non zero. You can save space by only representing the non-zero values.
- Standard reresentation: all elements are explicitly represented in memory
- Sparse representation: locations (row, col) and their values are stored.
Generating sparse matrix
sparse() converts regular matrices to sparse matrices.
full() is the oppposite.
speye() generates a sparse identity matrix
sprand() generates a sparserandom matrix.
Add elements using S(3, 7) = -5
Interrogating sparse matrixes
nnz: number of non-zero elements
nonzeros: a column of the non-zero elements
spones: replace all non-zero elements with ones
spalloc: allocate space for a sparse matrix
issparse: true if the matrix is sparse
spfun: apply function to non-zero elments
spy: graphically represent sparsity
Cell Array
Arrays of data strctures.
- Data structures are represented by placing braces {} around a MATLAB array object. They can encapsulate other MATLAB arrays of various types and lengths.
Usage
()parentheses to reference the data structure itself:cells(1,1) ={[1,2,3]}{}braces to access the array in the data strcture:cells{1,2} = 'hello there'- Preallocate a cell array by using the cell function:
mycell = cell(3,3) - Or, use braces to construct a cell array:
thiscell = {[1 2;3 4], 'bird'; [34 21], eye95)+i.*ones(5)} - Extend by using indexing:
thiscell{3,2} = 'More!' - Gaps are filled with the empty array.
View
- Type the name of the cell array to display a description of the data structure i each element. Or, use
celldisp(thiscell)function to view full conetents. cellplot(thiscell)generates a graphical representation.
Access
Access data inside cell arrays by indexing the structure then the element: thiscell{2,1}(2).
Update
Delete rows or columns by setting them equal to the empty matrix: thiscell(2,:)=[]
As Strings
Strings are difficult to store in arrays because each row must have the same number of columns. Use cell arrays to get around this problem
stringlist = ['hi'; 'byebye'] % illegal
stringlist= ['hi '; 'byebye']
stringcell = {'hi'; 'byebye'}
Convert between the two representations with
stringlist = char(stringcell)
stringcell = cellstr(stringlist)
As Function Arguments
A variable number of function input/output arguments can done using a dummy argument,varagin. Place it last on the listof input arguments.
nargin can determine the total number of input arguments.
function doit(str1, varagin)
for n = 1:nargin-1
disp([str '' varagin{n}]);
end
For output, use varargout last in the list of output arguments. nargout determines the tortal number of output arguments requested by the user.
function varargout = timestable(num)
for n:1nargout
varargout{n} = [num2str(num `x` num2str(n) '=' num2str(num*n)];
end
Structures
Structures are another way of putting together unlike data. Each element in a structure is called a field.
- A structure is like a record in a database.
- E.g., a student may have a name, id, and gpa.
Creation Construct an array of structures by adding a subscript:
strudent.name =
student.name = 'Robin'
student.id = 2343465
student.gpa = 3.2
student(2).name = 'Maid'
student(2).id = 386748
student(2).gpa = 3.90
student(2).exams = [100 69 80]
- Whenever a new field is added to an element, it is added to all elements. Unfilled elements contain the empty array.
Allocate a structure or array of structures with
student(2) = struct('name', 'Maid', 'id', 98473245, 'gpa', 2.9);
student[50] = ('name', [], 'id', [], 'gpa', [], 'exams', []);
Updating
Remove a field with rmfield(studnet, 'id')
setfiled() function can be used to add data to a structure
student = setfield(student, {1}, 'exams', [78 98 88]);
Dynamic field names also work:
studnet(1).('exams') = [87 94 88];
Accessing
Reference data within an element using multiple subscripting: studnet(2).exams(2) = 99;
Extract all the data from a single field using bracket notation: gpalist = [student.gpa]
getfield() function extracts data from a structure
agpa = getfield(student, {2}, 'gpa');
anexam = getfield(student, {2}, 'exams', {3});
Dynamic field names:
agpa = studnet(2).('gpa');
studnet(1).('exams') = [78 98 88];
Nested Fields in structures can be other structures.
student(1).class(1).num = '520.123';
student(1).class(1).name = 'Computational Modelling for ECE';
class(1).roster = {'Robin Hood', 'Maid Marion', 'Friar Tuck'}
Function Handles
Contains information used in referencing a function. Once such a handle is created, it can be used to execute the function at any time, even if it is a nested function or subfunction.
- Note: if the function has no input parameters, it must still be followed by empty parentheses.
- Each handle has its own independent workspaces that remains in existence as long as the handle does.
Creation
hand = @functionname;
hand = str2func('functionname');Linked from Computational Modeling