Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT.

Similar presentations


Presentation on theme: "CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT."— Presentation transcript:

1 CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT

2 Arrays One dimensional arrays: indexed list of numbers For example, A = [ 1 2 4 8 16 ] A[1] = 1 A[5] = 8 How do we create arrays in MATLAB?

3 Arrays Arrays and vectors are the same thing in MATLAB Row vector A = [ 1 3 5 7 11 13 ] Column vector A = [ 1 3 5 7 11 13 ] Column vectors are created using the command A = [1; 3; 5; 7; 11; 13]

4 Variables in MATLAB All variables in MATLAB are arrays –Scalar is array with one element –Vector is array with one column or row –Matrix is array with many columns and rows There is no need to define the size of an array A variable can be changed to any other type in MATLAB (by adding or deleting elements)

5 Arrays Say you wanted to create a large array of even numbers between 0 and 100 (inclusive of endpoints) A = [ 0 2 4 6 8 …. --- Cumbersome to fill out You can do it faster using A = [ m : q : n ] where m = first number, q = spacing, n = last number

6 Arrays Typing A = [ 0 : 2 : 100 ] will generate all evens between 0 and 100 If you omit the q variable the default spacing is 1 Another example: A = [ -10 : -5 : 10 ] produces A = [ -10 -5 0 5 10 ]

7 Arrays How to generate an array with n elements with the first and last element specified? A = linspace(xi, xf, n) For example A = [0, 8, 6] will produce 6 numbers between 0 and 8. Spacing will be (8- 0)/5=1.6 A = 0 1.6 3.2 4.8 6.4 8.0

8 Two Dimensional Arrays How to generate a 2-D matrix? A = [ 1 st row ; 2 nd row; … last row ] For example A = [ 1 2 3 ; 4 5 6 ; 7 8 9 ] produces A = [ 1 2 3 4 5 6 7 8 9]

9 Two Dimensional Arrays You can also type A [ 1 2 3 4 5 6 7 8 9 ] and this produces the same array

10 Two Dimensional Arrays Using variables in arrays: a = 1; b = 2; c = 4; A = [ a, b, c ; a*b, sqrt(c), log(a) ] Expressions are separated by commas Using linspace in matrices A = [1:2:11; 0:5:25; linspace(10,60,6); 1 2 3 4 5 6]

11 Built-in functions Zeros: produce a zero matrix of dimensional m x n (m rows and n columns). For example, zeros(2, 3) produces the matrix: 0 0 0 Similar for ones(2, 3) Identity matrix: diagonal of ones created by eye(n) command

12 Transpose operator Transpose of a vector or matrix is computed using the ‘ (single quote) character. Transpose switches rows into columns (or vice versa) For example define A = [ 1 2 3]. Then transpose of A is indicated by A’ and computed as B = A’ B = [ 1 2 3 ]

13 Transpose on matrix Define matrix A as A = [ 1 2 3 4 ; 10 11 12 13 ] Transpose A using the quote operator and store in B (using the command B = A’) B = [ 110 211 312 413]

14 Addressing elements of an array or matrix If we define A as A = [ 10 20 40 50 ] then to access the first entry we use the operator A(1) To change the first entry 10 to 20 we type A(1)=20; Similar rules apply for a matrix. If A = [1 2 3 ; 4 5 6 ; 7 8 9 ] then A(1,2) has value 2, and A(2,3) has value 6

15 Addressing large chunks in an array A(:) refers to all elements of an array A(m:n) refers to elements m through n. For example if A = [ 1 3 5 7 11 13 17 ] then A(3:5) has value [ 5 7 11 ]

16 Addressing large chunks in a matrix A(:,n) refers to elements in column n. For example if A = [ 1 2 3 ; 4 5 6 ; 7 8 9 ] then A(:,2) has value [ 2 5 8 ] A(n,:) refers to elements in row n. So A(3,:) has value [ 7 8 9 ] A(m:n,p:q) refers to all elements in rows m through n and columns p through q. So A(2:3,1:2) has value [ 4 5 ; 7 8 ]

17 Adding elements to an array A vector can changed to have more elements. For example if A = [ 1 2 3 4 ] then A(5:10) = 10:10:50 updates A to be A = [ 1 2 3 4 10 20 30 40 50 ] If we type A(10) = 100 then 0’s are padded A = [ 1 2 3 4 0 0 0 0 0 10 ] Two vectors can also be appended using the operation A = [ B C ]

18 Adding elements to a matrix Addition to a matrix is performed similarly to the vector. If A = [ 1 2 3 ; 10 20 30 ] then typing A(3,:) = [ 4 5 6 ] updates A to A = [ 1 2 3 10 20 30 4 5 6 ]

19 Adding elements to a matrix If you add an element into a matrix zeros will be padded in. For example, if A = [ 1 2 3 ; 10 20 30 ] then typing A(4,5) = 10 updates A to A = [ 1 2 300 10 20 3000 4 5 6 00 000010]

20 Adding elements to a matrix If you add an element into a matrix zeros will be padded in. For example, if A = [ 1 2 3 ; 10 20 30 ] then typing A(4,5) = 10 updates A to A = [ 1 2 300 10 20 3000 4 5 6 00 000010]

21 Deleting elements from array or matrix Deleting is the same as updating to an empty array If A = [ 10 12 14 16 18 ] then A(2) = [] deletes the second element. The new array is now A = [ 10 14 16 18 ] MATLAB automatically updates the size of the arrays

22 Built-in functions for arrays length(A) returns number of element in vector A size(A) returns dimensionality of array or matrix reshape(A, m, n) changes dimensions to m times n diag(v) creates a matrix with diagonal = vector v diag(A) produces the diagonal of matrix A

23 Strings A string is an array of characters created by typing characters enclosed in quotes For example ‘MATLAB’, ‘hello world’, ‘3^*2’ Define A = ‘I love this class’ If you type A(3:6) = ‘like’ then A becomes ‘I like this class’ Note that x = 10 and x = ’10’ are not the same. x = ’10’ cannot be used in mathematical expressions


Download ppt "CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT."

Similar presentations


Ads by Google