Programming with Matlab Day 2: More about Loops, Vectors and Matrices.

Slides:



Advertisements
Similar presentations
Introduction to Matlab
Advertisements

Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 3.
Maths for Computer Graphics
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
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.
Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections.
4.2 Adding and Subtracting Matrices 4.3 Matrix Multiplication
Row 1 Row 2 Row 3 Row m Column 1Column 2Column 3 Column 4.
Extending MATLAB Write your own scripts and/or functions Scripts and functions are plain text files with extension.m (m-files) To execute commands contained.
Matlab intro The Environment
Prelude A pattern of activation in a NN is a vector A set of connection weights between units is a matrix Vectors and matrices have well-understood mathematical.
2-1 Relations and 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.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
Introduction to MATLAB
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
1 Week 3: Vectors and Matrices (Part III) READING: 2.2 – 2.4 EECS Introduction to Computing for the Physical Sciences.
Row 1 Row 2 Row 3 Row m Column 1Column 2Column 3 Column 4.
1 Lab 2 of COMP 319 Lab tutor : Shenghua ZHONG Lab 2: Sep. 28, 2011 Data and File in Matlab.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
MATLAB Harri Saarnisaari, Part of Simulations and Tools for Telecommunication Course.
MAE 1202: AEROSPACE PRACTICUM An Introduction to MATLAB: Part 2 Mechanical and Aerospace Engineering Department Florida Institute of Technology Developed.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Introduction to MATLAB Session 1 Simopekka Vänskä, THL 2010.
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
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.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Introduction to Matlab Part II 1Daniel Baur / Introduction to Matlab Part II Daniel Baur / Michael Sokolov ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
Array Operations ENGR 1181 MATLAB 4.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
CSE 455 : Computer Vision MATLAB 101 Getting Started with MATLAB.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Array Accessing and Strings ENGR 1187 MATLAB 3. Today's Topics  Array Addressing (indexing)  Vector Addressing (indexing)  Matrix Addressing (indexing)
Intro to Matlab Rogelio Long September 3, How to access MyDesktop Log in with your utep id and password.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
CS 450: COMPUTER GRAPHICS TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE.
Finishing up Chapter 5. Will this code enter the if statement? G=[30,55,10] if G
PATTERN RECOGNITION LAB 2 TA : Nouf Al-Harbi::
EEE 242 Computer Tools for Electrical Engineering
Tutorial on Matlab Basics
Computer Application in Engineering Design
Introduction to Mat lab
Seminar 1 for DCSP Using Matlab.
Introduction To MATLAB
Multiplying Matrices.
Use of Mathematics using Technology (Maltlab)
Matlab tutorial course
CSCI N207 Data Analysis Using Spreadsheet
Communication and Coding Theory Lab(CS491)
Loop Statements & Vectorizing Code
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Multiplying Matrices.
Introduction to Matlab
-seminar 1 for digital signal processing
Loop Statements & Vectorizing Code
Multiplying Matrices.
Multiplying Matrices.
Multiplying Matrices.
Presentation transcript:

Programming with Matlab Day 2: More about Loops, Vectors and Matrices

MATLAB, PROS AND CONS: Pros: -Graphical Interface. -Many libraries of predefined functions (TOOLBOXes). -Easy to plot and represent graphical results. -Powerful and easy matrix and vector calculus. Cons: - Inefficient for exhaustive computation. - A Java based programming enviroment can be computationally expensive.

More About Loops! -While loop: Similar to the “for” loop but the number of times the statements in the body are executed can depend on the variable values that are computed in the loop. while CONDITION IS TRUE DO SOMETHING end Pseudocode - Exercise: Transform the “pesadito” script into a while loop (with the same output). >> rand 0.01 (generates random number between 0 and 1 ) >> ceil(1.9) 2 (upper round of the input number) Command Window

More About Loops! - Nested Loops: These are loops within loops. for i=1:6 for j=1:3 DO SOMETHING end Pseudocode - Exercises: 1)Write a script that creates a matrix (which will be explained on the next slides!) with the results of multiplying all the numbers from 1 to 5 between them. 2)Extra: Make this script not to compute the same pair twice. (hint: for j = i:N) i=1 i=2 i=3 i=4 i=5 i=6 j=1 j=2 j=3

Defining Vectors >> vec = [ ]; >> Command Window >> vec = [0:4] vec= >> vec = [1:3:12] vec= Command Window - Initializing vectors: - Vectors as a range of numbers: >> vec(4) ans = 10 >> vec(2:4) ans = >> vec(2:2:4) ans = 4 10 Command Window - Accessing vector elements or ranges:

Manipulating vector elements >> v = [1 8]; >> v = [v 3 4] v = >> w = [5 3]; >> q = [4 w] q = Command Window - Adding elements >> v = [ ]; >> v(3) = 1 v = >> v(2:3)=[9 9] v = Command Window - Changing single elements

Defining Matrices >> M = [ ; ] M = Command Window >> M = [ 1:8 ; 9:16 ; 17:24 ; 25:32] M = Command Window - Initializing matrices: - Matrices as a range of data: >> M (2,5) 13 >> M(3,:) >> M(:,3) >> M(3,1:3) Command Window - Accessing matrix elements or ranges:

Some Special Matrices >> ones 1 >> ones(2,3) >>zeros(1,3) >>Inf(3,2) Inf Inf >>NaN(2,1) NaN Command Window >> 1/0 Inf >>Inf/Inf NaN >>NaN+34 NaN >>mean([3 4 2 NaN 30]) NaN >>mean([3 4 2 Inf 30]) Inf Command Window - Inf: Infinity and NaN: Not-A-Number 2 rows, 3 columns The same for -,*,/ results too large to represent undefined numerical results

Operating with vectors and matrices - Basic operations (element-by-element): >> v=[1:4] v = >> v* >> v >> v / 6; >> v * 5.5; Command Window Scalar operations are performed element by element. >> M = [1 2 ; 3 4 ] >> N = [2.5 3 ; 2 1] M = >> M+N >> M.*N Command Window The dot indicates an element-by-element operation >> M./N; >> M.^2; N =

Basic functions >>v=[ ] v = >> length(v) 4 >> max(v) 8 >> sum(v) 17 >> mean(v) 4.25 Command Window >> M=[ ; ] M = >>size(M) 2 4 >>length(M) 4 >> max(M) >> sum(M) >> mean(M) Command Window length of largest dimension >> min(v) 2 >> std(v) For matrices:- For vectors: operating along columns

-“Plot” function: 2D Graphical Display Values for X´s coordinates plot( X coord´s, Y coord´s,’properties’) Values for Y´s coordinates Graphic properties (eg. ‘-.r’) plot(2,1, '*') plot( [1 2 5/2], [ 1/2 1 2 ],‘.-k')

-Exercise : 5)Make a script which represents graphically the following functions: i.sin(x) ii.sin(2x) iii.sin(3x) iv.sin(4x) 0 < x < 4 π (interval size 0.1) Vectors and loops should be used!! Try with “hold on” >> x = [0:0.1:100]; >> y = log(x); >> plot(x,y,’--r’); >> xlabel(‘blablabla’); >> ylabel(‘blebleble’); >> title(‘My Plot’); >> axis([xmin xmax ymin ymax]) >> hold on >> plot(x,’.b’) >> hold off Command Window - Example: 2D Graphical Display Style and color

Aim: Given a protein sequence, we want to display an alignment against itself, in order to identify graphically similar regions inside the sequence. Sequence = [‘AAAEYDSLEYDSLGYENEAAAEYDSLEYDSLGYENE’] Exercise of the day! Protein sequence alignment Hint 1: We want to compare all against all residues Hint 2: Question to answer: i == j => 1 i != j => 0 Hint 3: Binary matrix needed… Hint 4: Try to display the result graphically with “imagesc(Matrix)” ! A A A E Y D …. A A A E Y D

Don’t give up, coding can be fun!