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

Slides:



Advertisements
Similar presentations
2.3 Modeling Real World Data with Matrices
Advertisements

Maths for Computer Graphics
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
EGR 106 – Week 3 – More on Arrays Brief review of last week Additional ideas: – Special arrays – Changing an array – Some array operators – Character arrays.
Matrix Mathematics in MATLAB and Excel
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Matrices The Basics Vocabulary and basic concepts.
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
ECON 1150 Matrix Operations Special Matrices
Matlab tutorial course Lesson 2: Arrays and data types
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
Matlab Chapter 2: Array and Matrix Operations. What is a vector? In Matlab, it is a single row (horizontal) or column (vertical) of numbers or characters.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
Array Addition  Two arrays can be added if and only if both arrays have exactly the same dimensions.  Assuming the dimension requirement is satisfied,
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
Learner’s Guide to MATLAB® Chapter 2 : Working with Arrays.
Row 1 Row 2 Row 3 Row m Column 1Column 2Column 3 Column 4.
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
Chapter 4 Review: Manipulating Matrices Introduction to MATLAB 7 Engineering 161.
Matlab for Engineers Manipulating Matlab Matrices Chapter 4.
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
Working with Arrays in MATLAB
A string is an array of characters Strings have many uses in MATLAB Display text output Specify formatting for plots Input arguments for some functions.
Prepared by Deluar Jahan Moloy Lecturer Northern University Bangladesh
>> x = [ ]; y = 2*x y = Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions.
Chapter 2 Creating Arrays Legend: MATLAB command or syntax : Blue MATLAB command OUTPUT : LIGHT BLUE.
Meeting 18 Matrix Operations. Matrix If A is an m x n matrix - that is, a matrix with m rows and n columns – then the scalar entry in the i th row and.
Part 1 Chapter 2 MATLAB Fundamentals PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and Prof. Steve Chapra, Tufts University All.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
Array Creation ENGR 1187 MATLAB 2. Today’s Topics  Arrays: What are they?  Vectors  Matrices  Creating Arrays.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
CMPS 1371 Introduction to Computing for Engineers VECTORS.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
Precalculus Section 14.1 Add and subtract matrices Often a set of data is arranged in a table form A matrix is a rectangular.
An Introduction to Programming in Matlab Emily Blumenthal
Relational and Logical Operators EE 201 1C7-2 Spring 2012.
Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
ENG College of Engineering Engineering Education Innovation Center 1 Arrays in MATLAB Topics Covered: 1.Creating arrays of numbers vectors matrices.
LAB 2 Vectors and Matrices Dr.Abdel Fattah FARES.
ECE 1304 Introduction to Electrical and Computer Engineering
Linear Algebra review (optional)
Chapter 7 Matrix Mathematics
Chapter 2 Creating Arrays.
Unit 1: Matrices Day 1 Aug. 7th, 2012.
EGR 115 Introduction to Computing for Engineers
Introduction to Programming for Mechanical Engineers (ME 319)
Matrices Elements, Adding and Subtracting
Vectors and Matrices I.
Introduction to MATLAB [Vectors and Matrices] Lab 2
CSCI N207 Data Analysis Using Spreadsheet
Chapter2 Creating Arrays
Array and Matrix Functions
Arrays and Matrices in MATLAB
Matrices in MATLAB Dr. Risanuri Hidayat.
EECS Introduction to Computing for the Physical Sciences
1.8 Matrices.
1.8 Matrices.
Working with Arrays in MATLAB
Presentation transcript:

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

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

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

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)

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

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 = [ ]

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 =

Two Dimensional Arrays How to generate a 2-D matrix? A = [ 1 st row ; 2 nd row; … last row ] For example A = [ ; ; ] produces A = [ ]

Two Dimensional Arrays You can also type A [ ] and this produces the same array

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); ]

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: Similar for ones(2, 3) Identity matrix: diagonal of ones created by eye(n) command

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 = [ ]

Transpose on matrix Define matrix A as A = [ ; ] Transpose A using the quote operator and store in B (using the command B = A’) B = [ ]

Addressing elements of an array or matrix If we define A as A = [ ] 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 ; ; ] then A(1,2) has value 2, and A(2,3) has value 6

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 = [ ] then A(3:5) has value [ ]

Addressing large chunks in a matrix A(:,n) refers to elements in column n. For example if A = [ ; ; ] then A(:,2) has value [ ] A(n,:) refers to elements in row n. So A(3,:) has value [ ] 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 ]

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

Adding elements to a matrix Addition to a matrix is performed similarly to the vector. If A = [ ; ] then typing A(3,:) = [ ] updates A to A = [ ]

Adding elements to a matrix If you add an element into a matrix zeros will be padded in. For example, if A = [ ; ] then typing A(4,5) = 10 updates A to A = [ ]

Adding elements to a matrix If you add an element into a matrix zeros will be padded in. For example, if A = [ ; ] then typing A(4,5) = 10 updates A to A = [ ]

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

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

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