Introduction_to_Matlab_(Code)

Introduction_to_Matlab_(Code) - Introduction to Matlab...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Matlab (Code) intro.m %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Introduction to Matlab %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % (1) Basics % The symbol "%" is used to indicate a comment (for the remainder of % the line). % When writing a long Matlab statement that becomes to long for a % single line use ". .." at the end of the line to continue on the next % line. E.g. A = [1, 2; ... 3, 4]; % A semicolon at the end of a statement means that Matlab will not % display the result of the evaluated statement. If the ";" is omitted % then Matlab will display the result. This is also useful for % printing the value of variables, e.g. A % Matlab's command line is a little like a standard shell: % - Use the up arrow to recall commands without retyping them (and % down arrow to go forward in the command history). % - C-a moves to beginning of line (C-e for end), C-f moves forward a % character and C-b moves back (equivalent to the left and right % arrow keys), C-d deletes a character, C-k deletes the rest of the % line to the right of the cursor, C-p goes back through the % command history and C-n goes forward (equivalent to up and down % arrows), Tab tries to complete a command. % Simple debugging: % If the command "dbstop if error" is issued before running a script % or a function that causes a run-time error, the execution will stop % at the point where the error occurred. Very useful for tracking down % errors. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % (2) Basic types in Matlab %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % (A) The basic types in Matlab are scalars (usually double-precision % floating point), vectors, and matrices: A = [1 2; 3 4]; % Creates a 2x2 matrix B = [1,2; 3,4]; % The simplest way to create a matrix is % to list its entries in square brackets. % The ";" symbol separates rows; % the (optional) "," separates columns. N = 5 % A scalar v = [1 0 0] % A row vector v = [1; 2; 3] % A column vector v = v' % Transpose a vector (row to column or % column to row) v = 1:.5:3 % A vector filled in a specified range: v = pi*[-4:4]/4 % [start:stepsize:end]
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
% (brackets are optional) v = [] % Empty vector %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % (B) Creating special matrices: 1ST parameter is ROWS, % 2ND parameter is COLS m = zeros(2, 3) % Creates a 2x3 matrix of zeros v = ones(1, 3) % Creates a 1x3 matrix (row vector) of ones m = eye(3) % Identity matrix (3x3) v = rand(3, 1) % Randomly filled 3x1 matrix (column % vector); see also randn % But watch out: m = zeros(3) % Creates a 3x3 matrix (!) of zeros %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % (C) Indexing vectors and matrices. % Warning: Indices always start at 1 and *NOT* at 0! v = [1 2 3]; v(3) % Access a vector element m = [1 2 3 4; 5 7 8 8; 9 10 11 12; 13 14 15 16] m(1, 3) % Access a matrix element % matrix(ROW #, COLUMN #) m(2, :) % Access a whole matrix row (2nd row) m(:, 1) % Access a whole matrix column (1st column) m(1, 1:3) % Access elements 1 through 3 of the 1st row m(2:3, 2) % Access elements 2 through 3 of the % 2nd column m(2: end , 3) % Keyword "end" accesses the remainder of a % column or row m = [1 2 3; 4 5 6] size(m) % Returns the size of a matrix size(m, 1) % Number of rows
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

Introduction_to_Matlab_(Code) - Introduction to Matlab...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online