MATLAB for Neural Networks. The Quick and Dirty MATLAB Manual, ver0.2c.
General pointers. • MATLAB is both a language and an environment. You can
test ...
MATLAB for Neural Networks
The Quick and Dirty MATLAB Manual, ver0.2c General pointers • • •
MATLAB is both a language and an environment. You can test code snippets and run programs (“M-files”) from the interactive prompt. MATLAB is case-sensitive: i is not the same as I. MATLAB is case-sensitive. Learn how to figure things out. Programming is a creative exercise, and you will always encounter things that you don’t know how to do; programming is the art of figuring out how to do them. o Use the help command early and often. For example, to find the syntax for while loops, type help while. o Read error messages, they’re remarkably informative. They often include underlined links. o There are lots and lots of MATLAB tutorials and references available on the web, so find them and use them.
Data types • • •
All the usual suspects: integers, decimals, characters, and strings MATLAB doesn’t require (or allow) type declarations. Just define your variable and start using it. MATLAB will automatically upcast as necessary. Characters are evaluated by ASCII number. If you create a matrix containing numbers and characters, MATLAB will interpret the entire matrix as a character array. This is usually bad.
Scripting Writing scripts • A script is just a piece of saved code that might take some inputs and do something with them. Here’s an example of a short MATLAB script that adds two numbers and returns their sum: function [z] = add(x,y) % ADD performs addition, even though MATLAB does this natively % z = add(x,y) returns the sum of x and y z = x + y;
% create a variable z equal to the sum of x and y
•
Functions should begin with the line
•
function [output1,output2] = some_program(argument1,argument2) where outputs are outputs, some_program is the name of the program (e.g. ‘add’), and arguments are variable inputs to the function. Comments begin with the % sign.
Saving scripts • Save your programs as M-files such as some_program.m. The command to execute this program at the interactive prompt is some_program. • If MATLAB does not seem to notice when you edit your script, then make sure you have saved it and try again. • MATLAB should automatically recognize any M-files saved in your current directory; it might recognize new M-files saved elsewhere.
1 of 7
MATLAB for Neural Networks
• •
If you save a new M-file and MATLAB does not seem to notice, first check your spelling. Then try typing rehash toolboxcache in the command window. This tells MATLAB to check its directories for any file updates. If you save a program outside of your current directory or any of the other places where MATLAB would normally look, you may need to add that location to the path so that MATLAB can find it. Use addpath(‘some_drive:/some_directory’)
Creating matrices y = [3 5 9; 4 8 1] % The spacing makes it more readable Creates a 2x3 matrix, as shown, and names it ‘y’. Columns are separated by spaces, rows separated by semicolons Matrices are referenced in (row, column) format, e.g. y(1,2) Æ 5 y(1,:) Æ [3 5 9] y(:,2) Æ [5; 8] x = [] Creates an empty matrix (named ‘x’) x = [1:3] Creates a matrix of the numbers 1 through 3, in increments of 1, i.e. [1 2 3] x = [1:0.1:3] Same as above, but in increments of 0.1 x = [x;y] Append matrix y to matrix x Special matrices x = zeros(2,3) Creates a 2x3 matrix of zeroes x = ones(2,3) Creates a 2x3 matrix of ones z = eye(3) Creates a 3x3 identity matrix
Matrix operations y = y’ Transpose a matrix Flip matrices in various ways b = fliplr(a) % flip left-right b = flipud(a) % flip up-down b = rot90(a) % rotate 90 degrees b = flipdim(a, dim) % flip along specified dimension
2 of 7
MATLAB for Neural Networks
inv(z) Get the inverse of a matrix (A matrix multiplied by its inverse yields the identity matrix. You can only get the inverse of square matrices). length(x) Finds the length of a one-dimensional array size(y) Find the size of a multi-dimensional matrix [rows cols] find(x) Get the index locations of all non-zero elements find(x > 5) Get the index locations of elements greater than a certain value Perform matrix-wise arithmetic z = x + y Add z = x – y Subtract z = x * y Multiply z = x’ * y Perform inner product multiplication z = x * y’ Perform outer product multiplication z = x / y Divide! Perform element-wise arithmetic z = x .* y Multiply (element-wise) z = x ./ y Divide (element-wise)
Control structures Logical values 1. Booleans: As with most languages, 0 is false and 1 is true. There are a few additional gotchas when evaluating matrices.
3 of 7
MATLAB for Neural Networks
a.
All of the elements of a matrix must be non-zero for it to be true. For example: x = [1 0 1 1] if x
disp(‘x is true!’)
else end
disp(‘x is false :(‘) % this is a comment
b. Matrices must be of equal length/size in order to evaluate their equivalence. If they aren’t, you get a run-time error. c.
A string is implicitly a one-dimensional array of characters for comparison purposes ‘string’ == [‘s’ ‘t’ ‘r’ ‘i’ ‘n’ ‘g’]
2. Logical and relational operators a. & (and) b. | (or) c. ~ (not) d. == (equal) e. ~= (not equal) f. > (greater than) g. < (less than) h. >= (greater than or equal to) i.