1 / 25

Scientific Programming Using MATLAB, Fal 2010-2011

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

kieu
Télécharger la présentation

Scientific Programming Using MATLAB, Fal 2010-2011

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Scientific Programming Using MATLAB, Fal 2010-2011 TIRGUL #2: Variables

  2. Our to-do list • Variables • Arrays • scalars • vectors • matrices • Matrix transformations • Strings • mat-files

  3. What are variables? • Names that are used to store values • Allow • Operations on stored values • Saving them for later use

  4. Numerical arays • Scalars • Vectors • Matrices • Multi-dimensional

  5. Creating numerical arrays • By assignment: • myScalar = 1; • myVector = [1, 2, 3]; • myMatrix = [1, 2, 3; 4, 5, 6]; • You can always overwrite old variables

  6. 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

  7. 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

  8. 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

  9. 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)

  10. Caution! Orientation and size of matrices matter Adding matrices of different sizes  error Example: Tirgul2Arrays.m 6-7

  11. Managing variables • clear – remove all variables from workspace • clear var1 var2 • who– lists all variables in workspace • whos – adds more information

  12. 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

  13. 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

  14. 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

  15. Good scripting End lines with ; Add comments with % Cut long code lines with … Spacing

  16. 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'

  17. String and number conversions • num2str: converts numbers to strings • myStr = num2str(3.2)  myStr = ‘3.2’ • str2num: converts strings to numbers

  18. Strings - concatenation • We can compose complex strings using concatenation • Combining numbers in strings • Example: • numStudents = 32; • [‘The number of students is ’, num2str(numStudents)]

  19. Other string manipulations • length(myString) • strcmp(string1, string2) • strcmpi – case insensitive • ‘help strfun’ – list of all string-related functions • Example – Tirgul2Strings.m

  20. mat-files • Files with extension ‘.mat’ • Used to save variables from workspace

  21. ‘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

  22. ‘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

  23. 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

  24. 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.

  25. 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

More Related