EN 001-4: Introduction to Computational Design. Matrices & vectors. Why do we care about vectors? What is a matrix and a vector?

Similar documents
OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

Vectors and Matrices. Chapter 2. Linguaggio Programmazione Matlab-Simulink (2017/2018)

Matrices. A Matrix (This one has 2 Rows and 3 Columns) To add two matrices: add the numbers in the matching positions:

Square Roots: Introduction & Simplification

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

LAB 2 VECTORS AND MATRICES

Chapter 1: Number and Operations

MITOCW ocw f99-lec07_300k

FreeMat Tutorial. 3x + 4y 2z = 5 2x 5y + z = 8 x x + 3y = -1 xx

Basic matrix math in R

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

PetShop (BYU Students, SIGGRAPH 2006)

MITOCW ocw f99-lec12_300k

Matlab notes Matlab is a matrix-based, high-performance language for technical computing It integrates computation, visualisation and programming usin

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

MATLAB Tutorial Matrices & Vectors MATRICES AND VECTORS

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

MAT 003 Brian Killough s Instructor Notes Saint Leo University

Variable Definition and Statement Suppression You can create your own variables, and assign them values using = >> a = a = 3.

Math 1505G, 2013 Graphs and Matrices

Lab 4 CSE 7, Spring 2018 This lab is an introduction to using logical and comparison operators in Matlab.

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology.

The inverse of a matrix

Intro. Scheme Basics. scm> 5 5. scm>

Dr Richard Greenaway

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5

Matrix Inverse 2 ( 2) 1 = 2 1 2

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

Note: Please use the actual date you accessed this material in your citation.

Algebra 2 Common Core Summer Skills Packet

Table of Laplace Transforms

Vector: A series of scalars contained in a column or row. Dimensions: How many rows and columns a vector or matrix has.

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions.

Performing Matrix Operations on the TI-83/84

Introduction to Matlab

x = 12 x = 12 1x = 16

Array Creation ENGR 1181 MATLAB 2

Matlab for FMRI Module 1: the basics Instructor: Luis Hernandez-Garcia

MITOCW watch?v=9h6muyzjms0

Physics 326G Winter Class 2. In this class you will learn how to define and work with arrays or vectors.

Intro. Speed V Growth

Introduction to MATLAB

Skill 1: Multiplying Polynomials

MITOCW watch?v=zlohv4xq_ti

Section 1.1 Definitions and Properties

Teaching Manual Math 2131

Computing With R Handout 1

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013

Answers. Chapter 2. 1) Give the coordinates of the following points:

IDM 232. Scripting for Interactive Digital Media II. IDM 232: Scripting for IDM II 1

CS195H Homework 1 Grid homotopies and free groups. Due: February 5, 2015, before class

Computer Project #2 (Matrix Operations)

Lesson #17 Function Introduction

How to Improve Your Campaign Conversion Rates

Georgia Department of Education. Fifth Grade UNIT 1 STANDARDS

To add or subtract, just add or subtract the numbers in the same column and row and place answer accordingly.

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

APPM 2460 Matlab Basics

SUMMER REVIEW PACKET 2 FOR STUDENTS ENTERING ALGEBRA 1

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

Solution Guide for Chapter 12

Course Layout. Go to follow instr. Accessible within campus (only for the first download)

GENERAL MATH FOR PASSING

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu

Course Number 432/433 Title Algebra II (A & B) H Grade # of Days 120

Introduction to MATLAB

MITOCW watch?v=kz7jjltq9r4

MITOCW watch?v=r6-lqbquci0

Troubleshooting Maple Worksheets: Common Problems

ANSI C. Data Analysis in Geophysics Demián D. Gómez November 2013

QUICK EXCEL TUTORIAL. The Very Basics

CME 192: Introduction to Matlab

ARRAY VARIABLES (ROW VECTORS)

Getting To Know Matlab

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming


Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5

Only to be used for arranged hours. Order of Operations

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

MITOCW watch?v=flgjisf3l78

2: Functions, Equations, and Graphs

MITOCW watch?v=se4p7ivcune

Introduction to MATLAB

Teachers Teaching with Technology (Scotland) Teachers Teaching with Technology. Scotland T 3. Matrices. Teachers Teaching with Technology (Scotland)

Watkins Mill High School. Algebra 2. Math Challenge

Pre-Algebra Notes Unit 8: Graphs and Functions

MITOCW watch?v=w_-sx4vr53m

Heap sort. Carlos Moreno uwaterloo.ca EIT

Project 2: How Parentheses and the Order of Operations Impose Structure on Expressions

3D Transformations and Complex Representations. Computer Graphics CMU /15-662, Fall 2016

Introduction to MATLAB

CSE/Math 456 and CSE/Math 550 Matlab Tutorial and Demo

MITOCW watch?v=yarwp7tntl4

Therefore, after becoming familiar with the Matrix Method, you will be able to solve a system of two linear equations in four different ways.

Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That

MITOCW watch?v=0jljzrnhwoi

Square roots: We say that the square root of 16 is 4. We write this as 16 4.

Math 171 Proficiency Packet on Integers

Powered by. How did trying to give apples away for free change the world?

Transcription:

EN 001-: Introduction to Computational Design Fall 2017 Tufts University Instructor: Soha Hassoun soha@cs.tufts.edu Matrices & vectors Matlab is short for MATrix LABoratory. In Matlab, pretty much everything is actually a matrix. So one would expect Matlab to work well with matrices. By and large, that s true. But first, of course what is a matrix & why do we care? 1 2 What is a matrix and a vector? A vector is an ordered, indexed group of variables that share the same name. It's like a variable, but with a bunch of buckets in a row that are all numbered. Other languages call it an array. Example: vec1 = [1 3 7]; We use brackets to declare a vector but parentheses to access it! Then vec1(1) is 1; vec1(2) is 3; vec1(3) is 7. This is a row vector (your high-school math teacher may have also taught you about column vectors). Terminology: the individual elements of the vector are called, well, elements. Our vec1 has 3 elements. 3 Why do we care about vectors? A vector lets you collect a bunch of similar things, refer to them by one name, and operate on all of them at once. Consider the grades of a -student class for HW1: HW1_a=90; HW1 = [90,99,0,90]; HW1_b=99; HW1_c=0; HW1_d=90; Which format would you rather work with? What if there were 100 students rather than? But wait, there s more Consider the same deal for HW2: HW2 = [91,80,55,93]; HW2_a=91; HW2_b=80; HW2_c=55; HW2_d=93; What if you want the average? avg=(hw1+hw2)/2; Works no matter how many students there are! Yes it does! Avg_a=(HW1_a+HW2_a)/2; Avg_b=(HW1_b+HW2_b)/2; Avg_c=(HW1_c+HW2_c)/2; Avg_d=(HW1_d+HW2_d)/2; 5 Vector-scalar operations vec1=[1 3 7 5]; vec1 = vec1 * 1.1; Perhaps we re storing how much money everyone has, and they all got 10% interest. What do you think this would do? vec2 = vec1 + 1.1; 1

Vector-vector operations vec1=[1 3 7 5]; vec2=[2 2 1 1]; What do you think these would do? vec3 = vec1 + vec2; vec3 = vec1 * vec2; vec3 = vec1.* vec2; It s illegal to multiply two row vectors. We ll see why shortly The.* operator does element-by-element multiplication on two vectors Where else might vectors be useful? Any other ideas where vectors might be useful? Height of 100 patients (or weight) Temperature of each day of the year Really, most any time you want to keep track of a big bunch of items 7 8 More tricks with vectors vec1 = 2:; % same as [2 3 5 ] vec1 = 2:3:12; % same as [2 5 8 11] Hmmm this is the same notation as for i=2: for i=2:3:12 What do you think this code would do? vec1=2:; for i=vec1 stuff end exactly the same as for i=2: stuff end Under the hood, a for loop merely iterates through the elements of a vector! Just a reminder Let's write this down I've gotten it wrong about a hundred times already myself: You usually create matrices using brackets: mat=[1 2 3] You access matrices using parenthesis: mat(1) If you get it backwards, you'll get an error message that may not be incredibly clear. 9 10 Still more tricks with vectors vec1 = 2:3:12; vec1(1); vec1(3); vec1(2:3); numel(vec1); vec1(2)= ; vec1(2)=[]; vec1 = [1,2 3]; Concatenating vectors vec1=[1 3 7 5]; vec2=[2 2 1 1]; vec3 = [vec1 vec2]; vec3 = [1 3 7 5 vec2]; vec3 = [vec1 2 2 1 1]; 11 12 2

Strings are really vectors! A string is just a row vector str = 'abcd' str = ['ab' 'cd'] str=['a' 'b' 'c' 'd'] str=['ab' 'cd'] str=strcat('ab','cd') Deleting pieces There is an empty vector: []. vec1=[1 3 7 5 9 5]; vec1(3)=[]; vec1(2:3)=[]; Of course, the same works with strings name='wilbur'; name(:)=[]; name(3)='nnie'; 13 1 A bit of confusion disp(i) disp(i*i) That's because 'i' is the square root of -1 in math. i=3; disp(i) sqrt(-1) The motto: Matlab has a default meaning for 'i', but you can overrule it if you want. It works the same for vectors sqrt() sqrt = [2 8 10]; sqrt() Just like before, there is a default meaning for sqrt, but you can overrule it! The thing that "sqrt" refers to can change over time. another example: you can set var=1 and later set var='abc'; the same variable can change from number to string. 15 1 On to matrices A vector is just 1 dimension We showed a row vector; we ll also do column vectors. A matrix is 2 dimensions (a rectangular array) mat1 = [1 2 3; 5 ] yields Each ; starts a new row. 1 2 3 5 Why might we want matrices? What are some uses for matrices? 17 18 3

A matrix as a bunch of rows Column vectors We could do: HW1 = [90,99,0,90]; HW2 = [91,80,55,93]; Or just do HW= [90,99,0,90; 91,80,55,93] colvec = [1; 3; 7] 3x1 column vector It s just a matrix with 3 rows and one column. 1 3 7 19 20 Tricks with matrices Repmat HW= [90,99,0,90; 91,80,55,93]; size(hw) % numel(hw) % HW(2,3) % HW(:,2) HW(1,2:3) HW([1 2],[2 ]) Repmat() repeats a matrix. Assign my_mat=[1 5 3;2 1 ] repmat(my_mat, 2,1) repmat(my_mat, 1,2) repmat(my_mat,3,2) 1 5 3 2 1 21 22 It all starts to make sense Appending matrices, take 2 Remember when we did repmat ('*',1,3) to make '***'? '*' is a 1x1 matrix with (1)='*'. repmat('*',1,3) repeats that matrix 3 times gives us a matrix with (1)='*', (2)='*', (3)='*'. And this is just the string '***'. Similarly, repmat ('ab',1,2) 'abab' mat1=[1 2;3 ] mat2=[5 ; 7 8] [mat1 mat2] [mat1; mat2] 1 2 3 5 7 8 23 2

Not everything can be appended a=[1 2 3]; [1 2 3] b=[; 5; ]; [a b] 5 Matrix multiplication Review from high school (hopefully J) i th row, j th column = i th row j th column 1 2 3 * 2 = 28 (1*2 + 2* + 3*) 1 2 3 * 5 7 8 = 19 22 3 50 (e,g., 2 nd row 1 st column = (3 )*(5 7) = 3*5 + *7 = 3 25 2 More matrix multiplication Column vectors and transposes Not all arrays can be multiplied (the matrices must be conformal): [1 2 3] * [ 5 ] 1 2 3 * 5 = illegal 1 2 3 * 5 = illegal Why? Any row of [1 2 3] has 3 elements, but any column of [ 5 ] has only 1 element, so you cannot take their dot product. E.g., 1 st row, 1 st column would be (1 st row of [1 2 3])*(1 st column of [ 5 ]) = 1* + 2*? + 3*?. It doesn't work. 27 [1 2 3] * [; 5; ] [1 2 3] * [ 5 ]' 1 2 3 * 2 = 28 28 Transposes in general.* means element by element You transpose any matrix by switching rows & columns. So for any matrix my_mat, we have my_mat(r,c) = my_mat'(c,r) my_mat=[1 5 3;2 ] 1 5 3 2 my_mat' 1 2 5 3 [1 2 3] * [ 5 ] [1 2 3].* [ 5 ] 1 5 3 2 1 2 3 * 5 = illegal 1 2 3.* 5 = 10 18 1 1 2.* = 1 5 2 1 1 29 30 5

System of equations: 2x + 3y = 1 1x + 2y = 8 Matlab version a=[2 3; 1 2]; b = [1; 8]; Linear equations 2 3 1 2 a \ b % solves the equations and returns [; 2] Chooses between several different solvers, depending on the matrices you give it. x y = 1 8 Yeah, matrices might even be useful J Linear indexing Matlab's linear indexing lets you access a matrix with just one index. 1 3 5 7 Normal: HW= [90,99,0,90; 91,80,55,93]; HW(2,3) is 55 Linear indexing: HW(2) is 91, and HW(3) is 99 and HW(8) is 93, and HW([2 5]) is [91 0]. But why did they do this? Any thoughts? 2 8 31 32 The find() function vec=find (HW>90) % Returns a vector of indices of those elements that meet the condition HW(vec) % So we have an easy way to find elements of a matrix that meet a condition. Or, even easier, HW(find(HW>90)) yields [91 93 93]. sum() The sum() function sums up each column and returns a row vector. sum(hw) [181 179 115 183] It will also sum a column vector into 181 179 115 183 a single number (as expected) It will also sum a row vector into a single number! 90 91 90 91 181 181 33 3 Stuff to explore yourself A few more functions you can explore yourself : prod(): like sum(), but multiplies max(): like sum(), but takes the max inv(): matrix inversion ones(), zeros(), eye(): build some simple matrices Group activity; what does this code do? HW = HW+1 HW(find(HW<5)) = 70 HW(find(HW<5) = HW(find(HW<5)) + 5 HW(:, find(sum(hw)/2 <85)) = [] 35 3

Matrices and dog barf Uninitialized variables caused an error >> clear >> a Undefined function or variable 'a'. Does the same apply to matrices? >> clear >> b(2,3) Undefined function or variable 'b'. So, yes it does; but there's more to it than that. Initializing matrices a = [3,,9]; Creates a 1x3 vector and initializes it all at once. b = zeros(3,5); Creates a 3x5 matrix of zeros c(10)=1; what does this do? no possibility of eating dog barf 37 38 Social networks Let s talk about social networks. What are the big social-network sites? Do they have a concept of a friend? Is friend always symmetric? Are there different levels of friendship? How might we store the network? Our network will be simple Friendship is always symmetric You're either friends or not. That's all. Some thoughts on how to organize the data: A row vector. Each element is a string such as AmyN,BillJ (meaning that Amy and Bill are friends) A matrix. People get numbered. The friendship level between person #i and #j is stored at mat(i,j). Other ideas? 39 0 Group activity How might you find how many friends Dan has? How might you find out how many people have no friends at all? (hint: you don't need any loops) Ann Bill Cat Dan Ann 0 0 1 1 Bill 0 0 0 0 Cat 1 0 0 1 Dan 1 0 1 0 1 7