Download presentation
Presentation is loading. Please wait.
Published byCaroline Allen Modified over 6 years ago
1
Some Matlab Tips CSE 4309 – Machine Learning Vassilis Athitsos
Computer Science and Engineering Department University of Texas at Arlington
2
Basic Programming Constructs
Study the tutorial at: That can teach you how to do basic programming things, like: Conditionals Loops Functions Input/Output
3
M-Files Matlab code is stored in files with a .m extension.
These files are called M-files. There are two types of M-files: Scripts, that simply execute the code in them. Functions, that take arguments, produce a return value, and use a local namespace. In all programming tasks, you are asked to create a function. If you deliver a script instead of a function, you will lose points. You are free to create auxiliary M-files, scripts or functions, that are used by your top-level function.
4
Checking Number of Arguments
function result = foo(a, b, c, d) if nargin == 3 result = a+b+c; elseif nargin == 4 result = a*b*c*d; else error('foo takes 3 or 4 arguments'); end This function foo (saved in file foo.m) takes 3 or 4 arguments. Built-in variable nargin is used to check if 3 or 4 variables are passed.
5
Formatted Output a = 3; b = 2; str1 = sprintf('a = %6.4f, b = %3d\n', a, b); disp(str1); a = 3; b = 2; disp(sprintf('a = %6.4f, b = %3d\n', a, b)); To get formatted output, you use a combination of built-in functions disp and sprintf. You can combine these functions in a single line, as shown in the lower example.
6
Matrices a = zeros(3,4); rows = size(a, 1); cols = size(a, 2); disp(sprintf('the size of a is %dx%d', rows, cols)); Built-in function zeros can be used to create a matrix of specific size. Built in function size returns the size of the matrix. If the second argument is 1, size returns the number of rows of the first argument. If the second argument is 1, size returns the number of columns of the first argument.
7
Matrices a = zeros(3,4); a(1:2, 2:3) = 1; Result: a = You can assign values to entire rows, columns, or submatrices, as shown in the examples above.
8
Matrices You can define a range of indices using: a = zeros(3,4);
Result: a = You can define a range of indices using: a:b (all indices from a up to and including b). a:c:b (indices a, a+c, a+2c, a+3c, …, up to b (and including b if b is equal to a+N*c for some positive integer N). : means "entire row" or "entire column".
9
Matrices You can define a range of indices using: a = zeros(3,4);
Result: a = You can define a range of indices using: a:b (all indices from a up to and including b). a:c:b (indices a, a+c, a+2c, a+3c, …, up to b (and including b if b is equal to a+N*c for some positive integer N). : means "entire row" or "entire column".
10
Matrices You can define a range of indices using: a = zeros(3,4);
Result: a = You can define a range of indices using: a:b (all indices from a up to and including b). a:c:b (indices a, a+c, a+2c, a+3c, …, up to b (and including b if b is equal to a+N*c for some positive integer N). : means "entire row" or "entire column".
11
Matrix Operations mean(m) returns a vector, containing the mean of every column of m. std(m) returns a vector that contains the standard deviation of every column of m. cov(m) returns the covariance matrix of a set of N D-dimensional vectors, assuming that: m is a matrix of size NxD (N rows, D columns). Each row of m is one of the D-dimensional vectors. det(m) returns the determinant of m, if m is a square matrix.
12
Inverting a Matrix In several assignments, you need to invert matrices. inv(m) returns the inverse of a square matrix m. pinv(m) returns the pseudoinverse of a matrix m. m can be square or non-square. The pseudoinverse has the properties that: If m is of size R x C, then pinv(m) is of size C x R. m * pinv(m) * m = m. pinv(m) * m * pinv(m) = pinv(m). pinv is robust to matrices that are singular (non-invertible) or close to singular. Always use pinv.
13
Loading a Data File The data files that you are given for assignment 1 are text files, where each row corresponds to an example. The value in the last column is the class label. The rest of the values are the input vector. You can load this text file into a variable using this code: The code stores the data into matrix x, and then separates the vectors from the class labels. x = load('yeast_test.txt'); columns = size(x, 2); vectors = x(:, 1:(columns-1)); labels = x(:, columns);
14
Using Matlab Built-In Functions
Matlab has built-in functions that implement several concepts that are relevant to your assignments. Decision trees, neural networks, regression… I neither encourage nor forbid using such functions. If you want to understand the underlying concepts, it is best to not rely on built-in functions. If the built-in function does not do exactly what the assignment asks for, you are responsible for that. Built-in functions may use somewhat different formulas. Your solution should give results according to the formulas in the textbook and the slides, otherwise it will not be considered correct.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.