Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

Slides:



Advertisements
Similar presentations
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Advertisements

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.
Concatenation MATLAB lets you construct a new vector by concatenating other vectors: – A = [B C D... X Y Z] where the individual items in the brackets.
MOHAMMAD IMRAN DEPARTMENT OF APPLIED SCIENCES JAHANGIRABAD EDUCATIONAL GROUP OF INSTITUTES.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
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.
Matrix Definition A Matrix is an ordered set of numbers, variables or parameters. An example of a matrix can be represented by: The matrix is an ordered.
1 Introduction to MATLAB MATLAB is all of the following: 1.Computational environment 2.Plotting software 3.Programming language Typical applications: 1.Calculations.
Creating scalars, vectors, matrices Ex1 & 2. Dot Product & Cross Product Ex3. Plotting Graphs Ex4. Conversion Table Ex5. Plotting functions Finishing Ex4.
MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson.
1 Week 12 Arrays, vectors, matrices and cubes. Introduction to Scientific & Engineering Computing 2 Array subscript expressions n Each subscript in an.
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,
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
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.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
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 Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
Chapter 4 Review: Manipulating Matrices Introduction to MATLAB 7 Engineering 161.
Matlab for Engineers Manipulating Matlab Matrices Chapter 4.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
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.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
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.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
Array Creation ENGR 1181 MATLAB 2. Civil engineers store seismic data in arrays to analyze plate tectonics as well as fault patterns. These sets of data.
A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences.
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.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
Section 9-1 An Introduction to Matrices Objective: To perform scalar multiplication on a matrix. To solve matrices for variables. To solve problems using.
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
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.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
Matrices. Matrix - a rectangular array of variables or constants in horizontal rows and vertical columns enclosed in brackets. Element - each value in.
Matrix Algebra Definitions Operations Matrix algebra is a means of making calculations upon arrays of numbers (or data). Most data sets are matrix-type.
An Introduction to Programming in Matlab Emily Blumenthal
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
Windows Programming Lecture 03. Pointers and Arrays.
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.
Linear Algebra Review.
Manipulating MATLAB Matrices Chapter 4
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,
Chapter 2 Creating Arrays.
JavaScript: Functions.
Introduction to Matrices
Vectors and Matrices I.
Introduction to MATLAB [Vectors and Matrices] Lab 2
2.2 Introduction to Matrices
Arrays and Matrices in MATLAB
Array Creation ENGR 1181 MATLAB 02.
Matrices in MATLAB Dr. Risanuri Hidayat.
EECS Introduction to Computing for the Physical Sciences
Matrix Operations Ms. Olifer.
Working with Arrays in MATLAB
Presentation transcript:

Manipulating MATLAB Vector, Matrices 1

Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical values. You execute the assignment command to place the variable in the workspace memory (memory is part of hardware used for storing information). You are allowed to use the variable in algebraic expressions, etc. once it is assigned. 2

An array is MATLAB's basic data structure. Can have any number of dimensions. Most common are: vector - one dimension (a single row or column) matrix - two or more dimensions Arrays can have numbers or letters. (Arrays) 3

In MATLAB, a variable is stored as an array of numbers. When appropriate, it is interpreted as a scalar, vector or matrix. The size of an array is specified by the number of rows and the number of columns in the array, with the number of rows indicated first. Variables as Arrays scalar 1 × 1 vector n × 1 or 1 × n matrix n × m 4

Scalars are 1×1 arrays. They contain a single value, for example: X=3 Height=5.34 Width=10.09 Scalars 5

Vectors A vector is a list of numbers expressed as a 1 dimensional array. A vector can be n×1 or 1×n. Columns are separated by commas (or spaces): x = [1, 2, 3] or x = [1 2 3] Rows are separated by semicolons: y= [1; 2; 3] 6

To create a row vector from known numbers, type variable name, then equal sign, then inside square brackets, numbers separated by spaces. variable_name = [ n1, n2, n3 ] >> yr = [ ] yr = Note MATLAB displays row vector horizontally Commas optional (Creating a row vector) 7

To create a column vector from known numbers Method 1 - same as row vector but put semicolon after all but last number variable_name = [ n1; n2; n3 ] >> yr = [1984; 1986; 1988 ] yr = Note: MATLAB displays column vector vertically (Creating a Column Vector) 8

Method 2 - same as row vector but put apostrophe (') after closing bracket. Apostrophe interchanges rows and columns. Will come back on this later. variable_name = [ n1 n2 n3 ]' >> yr = [ ]' yr = (Creating a column vector) 9

To create a vector with specified constant spacing between elements variable_name = m:q:n m is the first number n is the last number q is the difference between consecutive numbers v = m:q:n means v = [ m m+q m+2q m+3q... n ] (Constant spacing vectors) 10

If q is omitted, the spacing is 1 v = m:n means v = [ m m+1 m+2 m+3... n ] >> x = 1:2:13 x = >> y = 1.5:0.1:2.1 y = Non-integer spacing (Constant spacing vectors) 11

>> z = -3:7 z = >> xa = 21:-3:6 xa = >> fred = 7:-2:15 fred = Empty matrix: 1-by-0 Negative spacing (Constant spacing vectors) Impossible spacing 12

1. How would you use linspace to set up spacings for planting seedlets in a garden row (30 seedlets, each row 2 meters long). 2. How about planning a 4285km road trip on 21 days. How long each leg would be? (Quick linspace Exercises) 13

To create a vector with specified number of terms between first and last v = linspace( xi, xf, n ) xi is first number xf is last number n is the number of terms (obviously must be a positivenumber) (= 100 if omitted) (Fixed length vectors) 14

>> va = linspace( 0, 8, 6 ) va = >> va = linspace( 30, 10, 11 ) va= m:q:n lets you directly specify spacing. linspace() lets you directly specify number of terms. Six elements Decreasing elements T I P 15

Indexing Into an Array allows you to change a value

Adding Elements

If you add an element outside the range of the original array, intermediate elements are added with a value of zero

2-D Matrices can also be entered by listing each row on a separate line C = [-12, 0, 0 12, 13, 40 1, -1, 90 0, 0, 2] 2-D Matrices

M = [12, 152, 6, 197, 32, -32, … 55, 82, 22, 10]; Use an ellipsis to continue a definition onto a new line 2-D Matrices

2-D matrix These semicolons are optional

Define a matrix using other matrices as components

Or…

Matrices A matrix is a two dimensional array of numbers. For example, this is a 4×3 matrix: mat1=[1.0,1.8,1.6; 3.0,- 2.0,25.1; 0.0,-5.1,12.7; 2.3,0.3,-3.1] Columns Rows 24

Indexed-location of numbers in an array Each item in an array is located in the (row, column). mat1(2,3) ans= Columns Rows 25

Enter the following into MATLAB: Scalar: x=90 Vectors: y=[1, 0, 55] z=[1 0 55] Matrix: m= [ 55, 44, 33; 0, 2, 88] Practice! 26

Vector, Matrices 27 Manipulating MATLAB

Defining (or assigning) arrays 28 An array can be defined by typing in a list of numbers enclosed in square brackets: Commas or spaces separate numbers. m=[12, 1, -33] or m=[ ] Semicolons indicate a new row. n=[2, 50, 20;1,1,2;0,-2,66] m= n =

p = Defining arrays continued 29 You can define an array in terms of another array: r=[m;n] p=[r,r] r = r =

We can access (read from or write to) elements in an array (vector or matrix) individually or in groups. Useful for changing a subset of elements. Useful for making a new variable from a subset of elements. (Verctor/Matrix Addressing) 30

(Accessing Vectors Elements 1) 31  You can access any element by indexing the vector name with parentheses (indexing starts from 1, not from 0 as in C). >> odd = [1:3:10] odd = >> odd(3) ans = 7

Review… 32 Index – a number used to identify elements in an array Retrieving a value from an array: n(2,1) ans=1 You can change a value in an element in an array with indexing: n(3,2)=55 n = n = You can extend an array by defining a new element: m=[ ] m(6)=9 m=[ ] Notice how undefined values of the array are filled with zeros

33  We can also access a range of elements (a subset of the original vector) by using the colon operator and giving a starting and ending index. >> odd(2:4) ans =  Simple arithmetic between scalars and vectors is possible. >> 10 + [1 2 3] ans = (Range of Elements / Vector Arithmetic)

(Matrices) 34 Create a two-dimensional matrix like this m = [ row 1 numbers; row 2 numbers;... ; last row numbers ] Each row separated by semicolon. All rows have same number of columns >> a=[ ; ; ] a =

(Matrices) 35 >> cd=6; e=3; h=4; >> Mat=[e, cd*h, cos(pi/3);... h^2 sqrt(h*h/cd) 14] Mat = Comma is optional

(Matrices) 36 Can also use m:p:n or linspace() to make rows Make sure each row has same number of columns >> A=[1:2:11; 0:5:25;... linspace(10,60,6); ] A =

(Matrices) 37 What if the number of columns is different? >> B= [ 1:4; linspace(1,4,5) ] ??? Error using ==> vertcat CAT arguments dimensions are not consistent. All arrys in the argument list must have the same number of columns. Four columns Five columns

(Special Matrix Commands) 38 zeros(m,n) - makes a matrix of m rows and n columns, all with zeros. ones(m,n) - makes a matrix of m rows and n columns, all with ones. eye(n) - makes a square matrix of n rows and columns. Main diagonal (upper left to lower right) has ones, all other elements are zero.

Examples! 39 zeros(2,5) ans = ones(3,5) ans = eye(5) ans = Note: Placing a single number inside either function will return an n × n array. e.g. ones(5) will return a 5 × 5 array filled with ones.

>> zr=zeros(3,4) zr = >> ne=ones(4,3) ne = >> idn=eye(5) idn =

To make a matrix filled with a particular number, multiply ones(m,n) by that number >> z=100*ones(3,4) z = T I P 41

(About variables in MATLAB) 42 All variables are arrays Scalar - array with only one element Vector - array with only one row or column Matrix - array with multiple rows and columns Assigning to variable specifies its dimension Don't have to define variable size before assigning to it, as you do in many programming languages Reassigning to variable changes its dimension to that of assignment.

(The Transpose Operator) 43 Transpose a variable by putting a single quote after it, e.g., x' In math, transpose usually denoted by superscript "T", e.g., xT Converts a row vector to a column vector and vice-versa. Switches rows and columns of a matrix, i.e., first row of original becomes first column of transposed, second row of original becomes second column of transposed, etc.

Continue… 44 The transpose operator, an apostrophe, changes all of an array’s rows to columns and columns to rows. m = [12, 1, -33] m’ m= ans= >> aa=[3 8 1] aa = >> bb=aa' bb = 3 8 1