Numeric, Cell and Structural Arrays One of the strenghts of MATLAB is the capabilty to handle collection of numbers called ARRAYS. MATLAB refers to scalars,

Slides:



Advertisements
Similar presentations
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
Advertisements

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 2 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.
Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections.
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.
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.
Builtin and user defined functions
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,
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU.
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,
Learner’s Guide to MATLAB® Chapter 2 : Working with Arrays.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
MATLAB An Introduction to MATLAB (Matrix Laboratory) 1.
1 MATLAB Basics. 2 MATLAB Documentation /help/techdoc/ Matrix Algebra.
1 Functions ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem
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.
Introduction to Matlab
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
>> 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.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
Introduction to Matlab Electromagnetic Theory LAB by Engr. Mian Shahzad Iqbal.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
Lecture 24: Rough and Ready Analysis. MATLAB uses function names consistent with most major programming languages For example sqrt sin cos log.
MATLAB Lecture 1 염익준. Introduction MATLAB (MATrix LABoratory) a special purpose computer program optimized to perform engineering and scientific calculations.
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)
MATLAB Constants, Variables & Expression Nafees Ahmed Asstt. Professor, EE Deptt DIT, DehraDun.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
Some “What’s the output” questions to get the day started… >>A = [1 2 3; 3 5 6] This statement stores the matrix: 1. A= 2. A= 3. A= 4. A= Ask Garvin’s.
Goals for today Learn to create, combine, and index arrays Learn to multiply arrays in MATLAB Use matrix multiplication to simulate a real-world problem.
Introduction To MATLAB
ECE 1304 Introduction to Electrical and Computer Engineering
Arithmetic Operations
Introduction to MATLAB for Engineers, Third Edition
Chapter 7 Matrix Mathematics
ECE 1304 Introduction to Electrical and Computer Engineering
Built-in MATLAB Functions Chapter 3
BIL 104E Introduction to Scientific and Engineering Computing
Chapter 3 Arrays and Vectors
EGR 115 Introduction to Computing for Engineers
Computer Simulation Lab
EEE 244 Numerical Methods In Electrical Engineering
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
7.3 Matrices.
Part 1 Chapter 2 MATLAB Fundamentals
INTRODUCTION TO MATLAB AM2032 JAYANTA MUKHERJEE.
Matlab tutorial course
Vectors and Matrices I.
Introduction to MATLAB [Vectors and Matrices] Lab 2
Digital Image Processing
Fourth Year – Software Engineering
Introduction to MATLAB
Arrays and Matrices in MATLAB
INTRODUCTION TO MATLAB
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Part 1 Chapter 2 MATLAB Fundamentals
Announcements P3 due today
How to Use MATLAB A Brief Introduction.
Arrays in Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
EECS Introduction to Computing for the Physical Sciences
CS100A Lecture 21 Previous Lecture This Lecture
Computer Simulation Lab
Presentation transcript:

Numeric, Cell and Structural Arrays One of the strenghts of MATLAB is the capabilty to handle collection of numbers called ARRAYS. MATLAB refers to scalars, vectors and matrices generally as arrays. The following classes of Array are now availble in MATLAB: ArrayNumericCharacterLogicalCellStructure Function handle Java

Numeric Arrays So far we have used only numeric arrays, which are arrays containing only numeric values. A character array is an array containing strings. The elements of Logical arrays are "true" or "false," which, although represented by the symbols 1 and 0, are not numeric quantities.

One Dimensional Array/ Vectors Row Vector Column Vector A vector is a special type of matrix, having only one row, or one column. Vectors are also called lists or arrays in other programming languages. Two Dimensional Array An array can have multiple rows or column or both, such a two dimensional array is called a matrix. Numeric Arrays

The official MATLAB documentation refers to all variables as arrays, whether they are single-valued (scalars) or multi-valued (vectors or matrices). In other words, a scalar is a 1x1 array, i.e. an array with a single row and a single column which, of course, is an array of one item. Rectangular Matrix: Scalar:1-by-1 array Vector:m-by-1 array 1-by-n array Matrix:m-by-n array Numeric Arrays

? X=[2 7 4] X=[2; 7; 4] or X=[2 7 4]’ X=[2 7 4;3 8 9] Y=[X X] Row Vector Column Vector Matrix or a 2D array Matrix of matrices Numeric Arrays

Vector & Matrix in MATLAB Rows (m) Columns (n) A = A (2,4) A (17) >>A=[ ]; >>A=[ ; ; ; ; ]; A (5) A(5,1) A (17)

More on Vectors x = start:endCreates row vector x starting with start, counting by 1, ending at end x = initial value : increment : final value Creates row vector x starting with start, counting by increment, ending at or before end x = linspace(start,end,number)Creates linearly spaced row vector x starting with start, ending at end, having number elements x = logspace(start,end,number)Creates logarithmically spaced row vector x starting with start, ending with end, having number elements length(x)Returns the length of vector x y = x’Transpose of vector x dot(x,y),cross(x,y)Returns the scalar dot and vector cross product of the vector x and y

Numeric Arrays Exercise Find Even Numbers between 1 to 10 >> A=[2:2:10] Fine Odd Numbers Between 1 to 10 >> B=[1:2:9] Find Even Numbers between 1 to 100 >> C=[2:2:100] Fine Odd Numbers Between 1 to 100 >> D=[1:2:99]

More on Matrices zeros(n) Returns a n ⅹ n matrix of zeros zeros(m,n) Returns a m ⅹ n matrix of zeros rand(m,n) Returns a m ⅹ n matrix of random numbers eye(m,n) Returns a m ⅹ n Identity matrix ones(n) Returns a n ⅹ n matrix of ones ones(m,n) Returns a m ⅹ n matrix of ones size(A) For a m ⅹ n matrix A, returns the row vector [m,n] containing the number of rows and columns in matrix length(A)Returns the larger of the number of rows or columns in A

Any MATLAB expression can be entered as a matrix element Entering Numeric Arrays »a=[1 2;3 4] a = »b=[-2.8, sqrt(-7), (3+5+6)*3/4] b = i »b(2,5) = 23 b = i »a=[1 2;3 4] a = »b=[-2.8, sqrt(-7), (3+5+6)*3/4] b = i »b(2,5) = 23 b = i Row separator: semicolon (;) Column separator: space / comma (,) Use square brackets [ ] Matrices must be rectangular/same height. (Set undefined elements to zero)

Any MATLAB expression can be entered as a matrix element Entering Numeric Arrays Row separator: semicolon (;) Column separator: space / comma (,) Matrices must be rectangular/same height. (Set undefined elements to zero) MATLAB does not allow this !

Entering Numeric Arrays - cont. »w=[1 2;3 4] + 5 w = »x = 1:5 x = »y = 2:-0.5:0 y = »z = rand(2,4) z = »w=[1 2;3 4] + 5 w = »x = 1:5 x = »y = 2:-0.5:0 y = »z = rand(2,4) z = Scalar expansion Creating sequences colon operator (:) Utility functions for creating matrices.

Array Addressing Array indices are the row and column numbers of an element in an array and are used to keep track of the array 's elements. For example, the notation v (5) refers to the fifth element in the vector v, and A (2, 3) refers to the element in row 2, column 3 in the matrix A. The row number is always listed first! This notation enables you to correct entries in an array without retyping the entire array, For example, to change the element in row I, column 3 of a matrix D to 6, you can type D (1, 3) = 6. Numeric Arrays

Array Subscripting / Indexing Array indices are the row and column numbers of an element in an array and are used to keep track of the array 's elements A = A(3,1) A(3) A(1:5,5) A(:,5) A(21:25) A(4:5,2:3) A([9 14;10 15]) Use () parentheses to specify index colon operator (:) specifies range / ALL [ ] to create matrix of index subscripts ‘end’ specifies maximum index value A(1:end,end) A(:,end) A(21:end)’

Element-by-Element Operations >>A=[1 2;4 5]; >>B=A*A % prints % Proper matrix multiplication >>B=A.*A % prints % Element by element multiplication

Element-by-Element Operations

Operations on Matrices TransposeB=A’ Identity Matrix eye(n) -> returns an n X n identity matrix eye(m,n) -> returns an m X n matrix with ones on the main diagonal and zeros elsewhere Addition and SubtractionC =A +B C =A - B Scalar MultiplicationB = α A, where α is a scalar Matrix MultiplicationC = A * B Matrix InverseB = inv(A), A must be a square matrix in this case Matrix powersB = A * A, A must be a square matrix Determinantdet(A), A must be a square matrix

Operations on Matrices

Operating on Matrices

Operating on Matrices - cont. Matrices must have the same dimensions Dimensions of resulting matrix = dimensions of multiplied matrices Resulting elements = product of corresponding elements from the original matrices Same rules apply for other array operations Array Multiplication »a = [ ; ]; »b = [1:4; 1:4]; »c = a.*b c = »a = [ ; ]; »b = [1:4; 1:4]; »c = a.*b c = c(2,4) = a(2,4)*b(2,4)

String Arrays Created using single quote delimiter (') Each character is a separate matrix element (16 bits of memory per character) Indexing same as for numeric arrays »str = 'Hi there,' str = Hi there, »str2 = 'Isn't MATLAB great?' str2 = Isn't MATLAB great? »str = 'Hi there,' str = Hi there, »str2 = 'Isn't MATLAB great?' str2 = Isn't MATLAB great? 1x9 vector str = Hithere,

Elemantary Mathematical (Trigonometric) Functions Trigonometric functions Remarks sin(x) cos(x) tan(x) asin(x) acos(x) atan(x) atan2(y,x) sinh(x) cosh(x) tanh(x) asinh(x) acosh(x) atanh(x) - pi/2 ≤ atan(x) ≥ pi/2, Same as atan(y/x) but –pi ≥ atan(y,x) ≥ pi Mathematical Functions of MATLAB-1

Other elemantary functions Remarks abs(x) angle(x) sqrt(x) real(x) imag(x) conj(x) round(x) fix(x) floor(x) ceil(x) sign(x) exp(x) log(x) log10(x) factor(x) Absolute value of x Phase angle of complex value: If x = real, angle = 0. If x = √-1, angle = pi/2 Square root of x Real part of complex value x Imaginary part of complex value x Complex conjugate x Round to do nearest integer Round a real value toward zero Round x toward - ∞ Round x toward + ∞ +1 if x > 0; -1 if x < 0 Exponential base e Log base e Log base 10 1 if x is a prime number, 0 if not Mathematical Functions of MATLAB-2

Numeric Arrays Assignment No 3 Assume Scalar Values of h, b, r, h1, h2 Find A tringle, A rectangle, A circle and A trapezidal b h b h r b h1 h2