CMPSC 201 Fall 2024 Lecture 22 PDF
Document Details
Uploaded by ErrFreeWendigo5560
Penn State
2024
Tags
Summary
This document contains lecture notes for CMPSC 201, Fall 2024, Lecture 22. The lecture covers MATLAB, including diary commands, script files, 2-D arrays, accessing elements, and indices out of range.
Full Transcript
CMPSC 201- Fall 2024 Lecture 22 November 13, 2024 Diary Command Allows you create a list of the commands (both good and bad) and recorded in a file. Start a diary by typing diary on or just diary and a file name if you want. End the recording by typing diary off...
CMPSC 201- Fall 2024 Lecture 22 November 13, 2024 Diary Command Allows you create a list of the commands (both good and bad) and recorded in a file. Start a diary by typing diary on or just diary and a file name if you want. End the recording by typing diary off. MATLAB Script File List of commands to complete a task that you can run over and over. What you will need to submit as for an assignment. For an assignment: Start with your name and section number and the commands clc and clear. Next write introductory comments for each problem in the assignment immediately before each problem specifying description, input, output, and processing. Follow with the commands to solve the problem. 2-D arrays Can create 2-D arrays by using the semicolon to separate the rows. Can combine existent row vectors. Accessing Elements in an Array In MATLAB, the indices (subscripts) are enclosed in parentheses. To access a specific element of a one-dimensional array (either a row or column vector) use the array name and the index (subscript) of the element of interest in parentheses. Can also access multiple elements using the colon operator to create a list of indices (subscripts) or hard code the list of indices (subscripts) to be used. lengths(2:7) – for lengths 2 through 7 lengths([2 5 1 8]) –for those 4 elements (note is does not need to be in ascending order) Indices Out of Range If you try display or to use an index that is out of range, then you will get an error message. However, you can expand the current size by using an assignment operator. Given the array amps = [3,11.3 6.7 9.1 45] The command x = amps(10) will generate an error The command amps(10) = 99 will change the row vector to [3,11.3 6.7 9.1 45 0 0 0 0 99] Accessing Elements in 2-D Array For a single element, enclose the row index and the column index in parentheses separated by a comma. arrayname(rowindex, columnindex) Can access multiple elements by specifying row and column indices. arrayname(1:4,6) arrayname(4,[5 1 6]) arrayname(4:2, 1:5) Questions ???