Matlab_II - Matlab Introduction to Programming Topics...

This preview shows pages 1–9. Sign up to view the full content.

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Matlab Introduction to Programming Topics covered Use of colon notation If statement Loops For While Creating and Executing Functions Writing data to files Reading data from files Use of Colon notation To create an array from 2 to 5 X = 2 : 5 (Matlab will assign its own increments) To create an array from 2 to 5, with increment of 0.5 Y = 2:0.5:5 Consider a 3 x 3 matrix M = [ 1 2 3; 4 5 6; 7 8 9]; a = (2,:) will assign the 2 nd row to a b = (:,3) will assign the 3 rd column to b If Statement At times, we may need to execute only certain parts of the program We use “if” statement in such situations. The most basic structure for an "if" statement is the following: if (condition statement) ( (program statement(s) ) end The condition statement and program statement(s) are Matlab commands The program statement(s) are executed only if the condition is satisfied If Statement More complicated “if” structures are written as: if (condition statement) (matlab commands) elseif (condition statement) (matlab commands) . . . else (matlab commands) end If Statement The condition statement is a relational or a logical statement Relational operators in Matlab < less than > greater than <= less than or equal >= greater than or equal == equal ~= not equal Note : "=" is used in an assigment statement while "==" is used in a relation Logical operators & and | or ~ not If Statement – An Example % Matlab program illustrating “if” statement clear all; a = 4; b = 4; if (a < b) j = -1; elseif (a > b) j = 2; else j = 3; end To see the value of j, type j and hit Enter! For Loop The "for" loop allows us to repeat certain commands...
View Full Document

{[ snackBarMessage ]}

Page1 / 19

Matlab_II - Matlab Introduction to Programming Topics...

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

View Full Document
Ask a homework question - tutors are online