Docsity

Dr. Cavazos' Lecture Notes: Unix Commands, IF Statements, Arrays, Loops, and Functions, Study notes of Computer Science

Partial preview of the text

Download Dr. Cavazos' Lecture Notes: Unix Commands, IF Statements, Arrays, Loops, and Functions and more Study notes Computer Science in PDF only on Docsity!

Dr. John Cavazos Computer and Information Sciences 03/16/

 Unix commands  if statements  arrays (or matrices)  loops  scripts  functions

 Write code from memory  Study labs ◦ Write code for labs  Study Midterm review  Attend review session

 When you log into a UNIX terminal ◦ You are in your home directory. ◦ To see the files in your directory.  ls ◦ To make an new folder/directory.  mkdir exampledir ◦ To change directories.  cd exampledir ◦ To go back one directory.  cd .. ◦ To go back to your home directory.  cd

if (expression1)

statements1;

elseif (expression2)

statements2;

else

statements3;

end

◦ A < B A is less than B ◦ A > B A is greater than B ◦ A <= B A is less than or equal to B ◦ A >= B A is greater than or equal to B ◦ A == B A is equal to B ◦ A ~= B A not equal B

if (N <= 5) fprintf 'blue\n'; end if (N > 5 & N <= 10) fprintf 'red\n'; end if (N > 10) fprintf 'green\n'; end

 All variables in matlab are arrays  An array of one element is called a scalar  A one dimension array is called a vector x=3.14;  scalar a = [1,2,3,4,5];  vector

 A = [1, 2; 3, 4; 5, 6]

 Creates a 3x2 array, 3 rows, 2 columns.  semicolon creates a new row.

A = 1 2

 Used when you know how many times code is to be executed.  Syntax for = ::  Variable is initially the start value  At end of iteration variable changes by increment  If value is not greater than end the loop runs again.

 total = 0; for i = 1:1:1000 loop starts at 1 total = total+widget (i); loop increments by 1 end loop ends at 1000 avg = total / 1000;

 The mail man/woman executes a loop.  If they know the number of deliveries  For loop for delivery = start : next_delivery : end deliver_mail(delivery) end

sumIt=0; for current=1:finish if (mod(current,2)==1) sumIt=sumIt+current; end end

 Special type of m-file ◦ Function name same as file name  Contains a function name, arguments, output, and “implementation”  All variables in function are local ◦ They are not visible outside call!