Computing Science 1P Large Group Tutorial 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.

Slides:



Advertisements
Similar presentations
Computing Science 1P Large Group Tutorial 19 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Quantitative Methods Session 16 – Matrices Pranjoy Arup Das.
4.1 Introduction to Matrices
Maths for Computer Graphics
General Computer Science for Engineers CISC 106 Lecture 25 Dr. John Cavazos Computer and Information Sciences 04/20/2009.
R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Main task -write me a program
Module 6 Matrices & Applications Chapter 26 Matrices and Applications I.
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
CE 311 K - Introduction to Computer Methods Daene C. McKinney
Computing Science 1P Large Group Tutorial 17 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Lecture 21: Friday 20 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Lecture 13: Friday 26 th January Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Lecture 18: Friday 2 nd March Simon Gay Department of Computing Science University of Glasgow 2006/07.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
What you will learn 1. What an identity matrix is
Lecture 7 Matrices CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
13.1 Matrices and Their Sums
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
COMPSCI 101 Principles of Programming Lecture 25 – Nested loops, passing mutable objects as parameters.
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.
Computing Science 1P Large Group Tutorial 20 Simon Gay Department of Computing Science University of Glasgow 2006/07.
When data from a table (or tables) needs to be manipulated, easier to deal with info in form of a matrix. Matrices FreshSophJunSen A0342 B0447 C2106 D1322.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Matrices Matrices A matrix (say MAY-trix) is a rectan- gular array of objects (usually numbers). An m  n (“m by n”) matrix has exactly m horizontal.
2009/9 1 Matrices(§3.8)  A matrix is a rectangular array of objects (usually numbers).  An m  n (“m by n”) matrix has exactly m horizontal rows, and.
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
Computing Science 1P Lecture 14: Friday 2 nd February Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Large Group Tutorial: Lab Exam & Class Test Simon Gay Department of Computing Science University of Glasgow 2006/07.
Discrete Mathematics 1 Kemal Akkaya DISCRETE MATHEMATICS Lecture 16 Dr. Kemal Akkaya Department of Computer Science.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
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.
Inverse of a Matrix Multiplicative Inverse of a Matrix For a square matrix A, the inverse is written A -1. When A is multiplied by A -1 the result is the.
Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
List comprehensions (and other shortcuts) UW CSE 160 Spring 2015.
3.6 Solving Systems Using Matrices You can use a matrix to represent and solve a system of equations without writing the variables. A matrix is a rectangular.
Loops and Simple Functions CS303E: Elements of Computers and Programming.
MATRIX A set of numbers arranged in rows and columns enclosed in round or square brackets is called a matrix. The order of a matrix gives the number of.
The Accumulator Pattern Summing: Add up (“accumulate”), e.g. 1 2 plus 2 2 plus 3 2 plus … plus Variation: Form a product (instead of sum), e.g.
Multidimensional Arrays Computer and Programming.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Chapter 4 Section 1 Organizing Data into Matrices.
Chapter 5: Matrices and Determinants Section 5.1: Matrix Addition.
CS 115 Lecture 17 2-D Lists Taken from notes by Dr. Neil Moore.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
Nested Loops CS303E: Elements of Computers and Programming.
12-1 Organizing Data Using Matrices
ECE 1304 Introduction to Electrical and Computer Engineering
Manipulating MATLAB Matrices Chapter 4
1.5 Matricies.
Sit-In Lab 1 Ob-CHESS-ion
2-D Lists Taken from notes by Dr. Neil Moore
Computer Graphics Matrix
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes for 2010: I skipped slide 10. This is.
Introduction to Matrices
Intro to Nested Looping
CSCI N207 Data Analysis Using Spreadsheet
Communication and Coding Theory Lab(CS491)
Lecture 13: Two-Dimensional Arrays
Intro to Nested Looping
Lets Play with arrays Singh Tripty
2-Dimensional Lists (Matrices) in Python
Matlab Basics Tutorial
Matrix A matrix is a rectangular arrangement of numbers in rows and columns Each number in a matrix is called an Element. The dimensions of a matrix are.
2-D Lists Taken from notes by Dr. Neil Moore
Chapter 4: Loops and Iteration
Presentation transcript:

Computing Science 1P Large Group Tutorial 13 Simon Gay Department of Computing Science University of Glasgow 2006/07

Computing Science 1P Tutorial 13 - Simon Gay2 What is len( [ 1, 2, [3, 4] ] ) ? Something else Don't know

2006/07Computing Science 1P Tutorial 13 - Simon Gay3 If x = [1, 2, [3, 4] ], what is x[2] ? 2 [3, 4] Something else Don't know

2006/07Computing Science 1P Tutorial 13 - Simon Gay4 If x = [ ["a", 3], ["b", 4] ], what is x[1][1] ? "a" 3 "b" 4 Something else Don't know

2006/07Computing Science 1P Tutorial 13 - Simon Gay5 If you type [0][0] into the Python shell, what is the result? Error message 0 [0] Something else Don't know

2006/07Computing Science 1P Tutorial 13 - Simon Gay6 Using nested lists to represent a matrix A matrix (plural: matrices) is a rectangular grid of numbers Represent it by a list of lists: [ [1,2,3], [2,4,5], [3,1,2] ] Matrices are ubiquitous in science and engineering (and computer graphics).

2006/07Computing Science 1P Tutorial 13 - Simon Gay7 Working with a matrix If m is a 3 by 3 matrix, what does the following code print? for i in range(3): print m[i][0] [ [1,2,3], [2,4,5], [3,1,2] ]

2006/07Computing Science 1P Tutorial 13 - Simon Gay8 What does the code print? The top row of the matrix The leftmost column of the matrix The diagonal of the matrix Something else Don't know

2006/07Computing Science 1P Tutorial 13 - Simon Gay9 Printing a matrix Define a function printMatrix which is given a parameter m, representing a 3 by 3 matrix, and prints it as a grid of numbers (no square brackets, no commas). Don’t worry about the spacing if the numbers have different numbers of digits.

2006/07Computing Science 1P Tutorial 13 - Simon Gay10 Printing a matrix def printMatrix(m): for row in m: for x in row: print x, print Key point: nested loops (to go with the nested lists).

2006/07Computing Science 1P Tutorial 13 - Simon Gay11 Trace of a matrix The trace of a matrix is the sum of the diagonal elements has trace 7 Define a function to calculate the trace of a given matrix.

2006/07Computing Science 1P Tutorial 13 - Simon Gay12 Trace of a matrix def trace(m): tr = 0 for i in range(len(m)): tr = tr + m[i][i] return tr Key point: m[i][i] is an element from the diagonal.

2006/07Computing Science 1P Tutorial 13 - Simon Gay13 Trace of a matrix: alternative def trace(m): tr = 0 i = 0 while i < len(m): tr = tr + m[i][i] i = i + 1 return tr But using for i in range(len(m)) seems nicer. Subtle point: for large numbers of iterations, range is less good because it builds a large list. Very subtle point: investigate xrange.

2006/07Computing Science 1P Tutorial 13 - Simon Gay14 Testing Someone has written a program which inputs a person’s age and is supposed to output a classification according to the following ranges: 17 or less:child 18 – 64:adult 65 or more:pensioner If the age is less than zero then an error message is supposed to be produced. Suggest input values which would be useful tests of the program.

2006/07Computing Science 1P Tutorial 13 - Simon Gay15 Testing Someone has defined a function which is supposed to return the most frequently occurring element of a list of numbers (in statistics this is called the mode). e.g. mode([1,2,1,1,3,4,3,2]) should be 1. Suggest useful test cases. (Further exercise: define mode ).