This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 14:440:127– Introduction to Computers for Engineers Notes for Lecture 3 Rutgers University, Fall 2008 Instructor Blase E. Ur 0.1 Input So far in this class, the main variability in the programs you’ve written has come from you, the programmers, by changing the value of variables you’ve set. In the real world, you’ll usually want to get input from the user, the person who is interacting with your program. For example, if you designed and built an ATM in Matlab, you’d ask the customer how much money they’d like to withdraw. Getting user input in Matlab uses the input function. There are two pieces of information you need to provide along with the input function: what variable to store the information in, as well as what information should be displayed to the screen in order to prompt the user (give them infor mation about what to enter). Here’s the syntax: money = input(’Please enter how much to withdraw: ’); 0.2 Creating Vectors and Matrices 0.2.1 Vectors Creating matrices or vectors (matrices with only 1 row OR 1 column) is quite easy in Matlab. You simply enclose a list of elements in square brackets. Here’s our first row vector (1 row, 4 columns): x = [ 1 5 8 9 ] To instead create column vectors, you need to indicate to Matlab to ”skip to the next row” in the vector. You use the semicolon ( ; ) to do this: y = [ 1; 5; 8; 9 ] 0.2.2 Matrices As you might guess, creating a Matrix is essentially the same as a vector. Don’t forget that the semicolon skips to the next row: z = [ 1 2 3; 4 5 6 ] This creates a 2x3 (2 rows by 3 columns) Matrix. Whenever we talk about the size of Matrices, the number of rows always comes first. 0.2.3 The Length/Size of a Vector/Matrix To find the size of a Matrix, you can use the size function. It returns a 1x2 vector containing the number of rows, and then the number of columns, in that matrix. You can either store the whole matrix in one variable, or use a special way of writing the command to store the number of rows and number of columns separately: % assume M is a 2x3 matrix z = size(M); [r c] = size(M); z would contain the matrix [2 3], r would contain the value 2, and c would contain the value 3. A vector only has one meaningful dimension, although you don’t always know whether it has only 1 row or instead only 1 column. Additionally, sometimes you only care about the largest dimension of a matrix rather than both the number of rows and columns. In this case, you can use the length function, which returns the larger of the number of rows and number of columns, and thus the length of a vector: % assume X is a vector, or a Matrix where only one dimension matters l = length(X) 0.3 Special Ways to Create Vectors/Matrices Matlab presents a number of more efficient ways to create certain types of vectors and matrices....
View
Full
Document
This note was uploaded on 11/09/2011 for the course ENGINEERIN 127 taught by Professor Finch during the Fall '08 term at Rutgers.
 Fall '08
 Finch

Click to edit the document details