Transcript PPT
General Computer Science
for Engineers
CISC 106
Lecture 04
James Atlas
Computer and Information Sciences
6/17/2009
Lecture Overview
If statements
Logical Operators
Matlab Output
Data Structures
Arrays
IF Statements
Review
IF Statements
Nested if statements can be used when
two things have to be true for a
command to execute
IF Statements
Using nested if
statements
if (x < 5)
if ( x > 2)
y = 500
else
y = 300
else
y= 200
end
IF Statements
Using nested if
statements
if (x < 5)
if ( x > 2)
y = 500
else
y = 300
else
y= 200
end
Using multiple
conditions in one if
statement
if ( x < 5 & x > 2)
y = 500
else
y = 200
end
Logical Operators - AND
&
&&
Logical Operators - OR
|
||
Logical Operators - NOT
~
Matlab Output
◦ How do we see information about what is going on
in our program?
Matlab Output
◦ How do we see information about what is going on
in our program?
◦ disp()
Matlab Output
Formatted Output
Example:
◦ “There are 5 widgets in inventory”
◦ fprintf(‘There are %d widgets in inventory’,
widgets);
Matlab Output
Formatted Output
Example:
◦ “There are 5 widgets in inventory”
◦ fprintf(‘There are %d widgets in inventory’,
widgets);
%d acts as a place holder for widget variable at end
of command and is known as a conversion
character.
Conversion characters specify the notation of the
output.
Note the variable widgets as the second argument.
Matlab Output
Lets look at the syntax
◦ count = fprintf(fid, format, A, ...)
◦ count – the number of bytes written
◦ fid – refers to opened file
◦ format – is a format string in between
single quotes
If fid is set to one or omitted the output goes
to the screen.
What if my program is too
complex?
Matlab Debugging
Break
Data Structures
So far we have only talked about single
points of data
Data Structures
Structured data
◦ Defines how multiple pieces of data
relate to each other
Data Structures
Structured data
◦ Defines how multiple pieces of data
relate to each other
We actually have covered one data
structure already
Arrays
Arrays
Arrays
Fundamental unit of data in MATLAB
Collection of data into rows and
columns
(MATrix LABoratory)
1 dimensional, vector
2 dimensional, matrix
0 dimensional, scalar (a 1x1 array)
Row vector, column vector , transpose
Arrays
Array commands
a = [1 2 3 4]
b = [1; 2; 3; 4]
c = [1 2; 3] (error)
d = [1 2; 3 4]
f = d(1,2)
g(4,5) = 7
Arrays continued
zeros(), ones(), eye()
scalar vs. array arithmetic
+, - , * (.*) , / (./) ^ etc.