250 likes | 362 Views
Scientific Programming Using MATLAB, Fal 2010-2011. TIRGUL #2: Variables. Our to-do list. Variables Arrays scalars vectors matrices Matrix transformations Strings mat-files. What are variables?. Names that are used to store values Allow Operations on stored values
E N D
Scientific Programming Using MATLAB, Fal 2010-2011 TIRGUL #2: Variables
Our to-do list • Variables • Arrays • scalars • vectors • matrices • Matrix transformations • Strings • mat-files
What are variables? • Names that are used to store values • Allow • Operations on stored values • Saving them for later use
Numerical arays • Scalars • Vectors • Matrices • Multi-dimensional
Creating numerical arrays • By assignment: • myScalar = 1; • myVector = [1, 2, 3]; • myMatrix = [1, 2, 3; 4, 5, 6]; • You can always overwrite old variables
Indexing • An index = an “address” of data within a variable • Can manipulate a certain part of it • A(i,j) = element in row i and column j of A
Useful commands • Size – returns array dimensions • Length – returns length of longest dimension • Numel – number of elements in array • Find • find(vector) indices of non-zero elements • find(vector == 4) indices of elements that have the value 4. • Example: Tirgul2Arrays 1-5
Matrix generation functions >> zeroMat = zeros(2,3) zeroMat = 0 0 0 0 0 0 • Special matrices: • zeros – matrix filed with zeros. • ones – same, with 1’s. • rand - random numbers, between 0 and 1 (uniform distribution). • randn - random numbers from a normal distribution (Gaussian), with mean = 0 and variance=std=1 • “Starting” variables for memory allocation
Operations on variables • Arithmetic operations: • +, -, *, / • ./ and .* for element by element operations • Built-in functions • exp(myVar), log(myVar), sqrt(myVar), sin(myVar) • Our own functions • myFunction(myVar) • Order of operations • 3*(exp(log(2)) + 2)
Caution! Orientation and size of matrices matter Adding matrices of different sizes error Example: Tirgul2Arrays.m 6-7
Managing variables • clear – remove all variables from workspace • clear var1 var2 • who– lists all variables in workspace • whos – adds more information
Matrix transformations • Transpose • myVec’ / myMat’ • transpose(myVec) • Reshape – change size&shape of variable, but keep content. • reshape(myMatrix, M, N) • Elements are taken column-wise • Example: vector = reshape(matrix, 1, 6) • Tirgul2Arrays.m 8
Variable names • Start with a letter • Up to 31 characters • May contain letters, digits and underscore_ • Meaningful • Case sensitive • Camel casing • thisIsAnExampleOfCamelCasing • Constants: upper-case letter, underscore between words. • Examples: BOLTZMANN_CONSTANT, SPEED_OF_LIGHT
Unassigned variable • When creating a variable without assignment, MATLAB automatically assigns it to a variable called ‘ans’ (short for answer) • Example: >> [1, 2, 3] • ans = 1 2 3 • You can assign it later on • Example: >> myAssignedVar= ans • myAssignedVar = 1 2 3
Good scripting End lines with ; Add comments with % Cut long code lines with … Spacing
Characters & Strings • Character = a printable/readable sign • Creation in MATLAB: ‘a letter’ • Examples: 'r' or '.' or ' ‘ • String = an array of characters • Creation in MATLAB: 'a few letters‘ • Example: A = 'this is a MATLAB string'
String and number conversions • num2str: converts numbers to strings • myStr = num2str(3.2) myStr = ‘3.2’ • str2num: converts strings to numbers
Strings - concatenation • We can compose complex strings using concatenation • Combining numbers in strings • Example: • numStudents = 32; • [‘The number of students is ’, num2str(numStudents)]
Other string manipulations • length(myString) • strcmp(string1, string2) • strcmpi – case insensitive • ‘help strfun’ – list of all string-related functions • Example – Tirgul2Strings.m
mat-files • Files with extension ‘.mat’ • Used to save variables from workspace
‘save’ command • Saving variables = creating a mat-file containing them, for later use • save(‘fileName’, ‘var1’, ‘var2’) • Default is saving entire workspace • ‘save’ will save the mat-file in your current directory
‘load’ command • Loading variables = reading them from a mat-file and adding them to workspace • load(‘fileName’, ‘var1’, ‘var2’) • Default is loading all variables in mat-file • ‘load’ will look for the file in your defined path • Caution: overrides existing variables with same names
Homework submission instructions • The following should be submitted: • Soft copy ( = code files): • Submission by email to: biumatlab@gmail.com • Email subject line: • exercise number- student name – id number • (example: Exercise 2 – NoaLiscovitch – 123456789) • Submission by Wednesday at 24:00 (you will have ~2 weeks for each exercise). • Hard copy ( = printed): • Hard copy should include: • Printed copy of your code (.m files). Please hand in allthe files in their final version (should be 100% identical to the soft copy version) • Submission at the beginning of the Tirgul
Homework submission instructions • Submission time - no exceptions or delays • All programs must run. If it does not run – no points! • Make sure that your files/functions/variables have exactly the required names as specified in the exercise • Don’t use ‘clc’ and ‘clear’ commands in the submitted code • Your programs should not display any output, unless specified • Line length - under 60 characters (use … to complete your command at the next line). • Don’t send your files using any archiving program (winzip, winrar, etc.) • Do the exercises on your own. You can talk with friends, but don’t work on the code together. • Please use only the material discussed up to the tirgul in which you were given the assignment, including. Full set of instructions is at the course website.
Practice • Create a row vector 1x4. • Add a scalar to the vector. • Change it into a column vector 5x1. • Create a matrix 2x4. • Create a 10x20 matrix of 3's. • Create the string ‘I like MATLAB'. • Use concatenation to change the string to ‘I like MATLAB very much'. • What is the length of this string? • Save your variables to a mat-file, clear them from the workspace and load them from your file