Introduction To Matlab Class 1

Slides:



Advertisements
Similar presentations
Introduction to Matlab
Advertisements

Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Matlab Intro Simple introduction to some basic Matlab syntax. Declaration of a variable [ ] Matrices or vectors Some special (useful) syntax. Control statements.
Introduction to MATLAB The language of Technical Computing.
Introduction to Matlab
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
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.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Introduction to Python
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
INTRODUCTION TO MATLAB LAB# 01
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
MATLAB Practice 1 Introducing MATLAB Lecture Notes on Video Search & Mining, Spring 2012 Presented by Jun Hee Yoo Biointelligence Laboratory School of.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
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 Class 4 Instructors: Hristiyan (Chris) Kourtev and Xiaotao Su, PhD Double click the matlab icon When prompted click “Skip”
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
SCRIPTS AND FUNCTIONS DAVID COOPER SUMMER Extensions MATLAB has two main extension types.m for functions and scripts and.mat for variable save files.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Whatcha doin'? Aims: To start using Python. To understand loops.
Introduction To Matlab Class 3
Prof. Mark Glauser Created by: David Marr
Introduction to Python
Introduction to Matlab
Matlab Training Session 4: Control, Flow and Functions
L – Modeling and Simulating Social Systems with MATLAB
Introduction to Programming
EGR 115 Introduction to Computing for Engineers
Chapter 4 MATLAB Programming
Variables, Expressions, and IO
Basic operations in Matlab
Introduction To Matlab Class 2
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
StatLab Matlab Workshop
Introduction To Matlab Class 7
Introduction to Programming
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Use of Mathematics using Technology (Maltlab)
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
T. Jumana Abu Shmais – AOU - Riyadh
Hello World! Syntax.
Introduction to Programming
INTRODUCTION TO MATLAB
CSCI N317 Computation for Scientific Applications Unit 1 – 1 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.
Functions continued.
Introduction to Programming
Introduction to Matlab:
Matlab Basics.
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Computer Simulation Lab
Presentation transcript:

Introduction To Matlab Class 1 Instructors: Hristiyan (Chris) Kourtev and Xiaotao Su, PhD Double click the matlab icon When prompted click “Skip”, then type: mkdir C:/MatlabClass/YourLastName cd C:/MatlabClass/YourLastName

http://ruccs.rutgers.edu/matlab_course/ Introduction Ask Questions! Slides, notes, assignments, and other info will be at: http://ruccs.rutgers.edu/matlab_course/

Setup Click Matlab mkdir C:/MatlabClass/YourLastName cd C:/MatlabClass/YourLastName

Programming Matlab has its own programming language What is a program? A series of instructions that the computer follows Also referred to as “Code” or a “Script”

Two Options For Where To Issue Commands/Instructions In the command window In a script (program) executed by the command window.

Note: The “_” character is the shifted dash SHIFT + - Hello World At the command prompt type: my_var = ‘hello world’; And press ENTER. Note: The “_” character is the shifted dash SHIFT + -

Hello World my_var = ‘hello world’; Semi-colon is so you don’t output the result to the screen. Single quotes denote character string Variable Name

Variables num_oranges = 3.5 num_apples = 7 7 3.5 my_var num_oranges my_var = ‘hello world’; num_oranges = 3.5 num_apples = 7 7 ‘hello world’ 3.5 my_var num_oranges num_apples Variable Name

Memory Your computer’s memory Each of the values are stored in memory associated with the variable name The “who” command will list the current variables in memory Typing my_var will return the value of that variable Var Name Value my_var ‘hello world’ num_apples 7 num_oranges 3.5 subject_name ‘john smith’ …

Setting values As you have seen you set the value of a variable with the “=“ sign You can also set a variable equal to another variable or the output of an operation on a variable.

Setting values new_var = my_var third_character = my_var(3) this is 7 fruit = num_apples + num_oranges this is now 10.5 num_apples = 6 what is the value of fruit now?

Changing around strings my_var is currently ‘hello world’ Typing my_var(3) will return ‘l’ my_var(3) = my_var(1) Now what is the value of my_var?

Calling Functions A function is a program that you call on to do some sort of action length(‘hello world’) length(my_var) disp(my_var)

disp( length(‘hello world’) ) Nesting Functions disp(length(my_var)) What happens here? disp( length(‘hello world’) ) disp(11) 11

Your first program What is pseudo-code and why we need it Typing commands one at a time isn’t very efficient, especially when you want to do the same ones repeatedly… so we create a program to do it This program will be called mix_strings

Comments Comments are descriptions of what is happening in your code. They are need to follow what is happening for debugging purposes At the beginning of a script you put in a type of comment called a header

% symbol makes this line “commented out” This means that the computer will ignore these lines These lines aren’t for the computer, they are for you % mix_strings.m % % Displays the string ‘hello world’ % Then scrambles letters in that string and % displays the scrambled string % written by XYZ 3/6/2005 Name of file Description: What does this file do? Who wrote it and when?

So what is the header good for? Save the file At the command line type: help mix_strings All matlab programs have headers. Find out information on disp, pause, and length

What else? lookfor can be used to search for programs that have certain words in their headers. lookfor string lookfor random

Back to the program clear all; my_var = ‘hello world’; mixstr = my_var; mixstr(3) = my_var(1); mixstr(1) = my_var(3); disp(mixstr);

Now run it In the command window: mix_strings

loops for i=1:5 disp(i) end; Used to repeat the same chunk of code many times for i=1:5 disp(i) end;

mix_strings2.m clear all; my_var = ‘hello world’; mixstr = ‘xxxxxxxxxx’; for i=1:length(my_var) mixstr(i) = my_var(i); end; disp(mixstr)

rand and ceil rand will create a random number from 0 to 1 ceil will round up any number to the next integer Together you can create random integers ceil(rand*7)

task 1 Replace each x in mixstr with a random character from my_var. Start with the first x, then the second x… ‘hello world’ ‘xxxxxxxxxxxxx’ Result could be -> ‘wdolhrdlhlwe’

% mix_strings3.m % %Displays the string 'hello world‘, then scrambles letters in that string %and displays the scrambled string % written by Chris Kourtev 7/12/2010 clear all; my_var = 'hello world'; mixstr = 'xxxxxxxxxxxxx'; disp(my_var); for i = 1:length(mixstr) mixstr(i) = my_var(ceil(rand*length(my_var))); end; disp(mixstr);

mat = [1, 5, 3; 2, 1, 5; 7, 9, 0] Vectors & Matrixes A string is a list of characters You can also have lists of numbers using vectors and matrixes vect = [1, 2, 5, 6, 3] mat = [1, 5, 3; 2, 1, 5; 7, 9, 0] mat = [1, 5, 3; 2, 1, 5; 7, 9, 0]

mat = [1, 5, 3; 2, 1, 5; 7, 9, 0] Vectors & Matrixes 1 2 5 6 3 1 5 3 2 vect = [1, 2, 5, 6, 3] mat = [1, 5, 3; 2, 1, 5; 7, 9, 0] mat = [1, 5, 3; 2, 1, 5; 7, 9, 0] 1 2 5 6 3 Vect mat 1 5 3 2 7 9 note: vectors are basically 1 dimensional matrixes

Getting Values out of Vectors & Matrixes 1 2 5 6 3 vect(3) returns 5 mat 1 5 3 2 7 9 mat(3,1) returns 7 Note: to get a cell you use (ROW, COLUMN)

vector as a 1D matrix vect = [1; 2; 3] vect2 = vect’ 1 2 3 1 2 3

transposing mat 1 5 3 2 7 9 1 2 7 5 9 3 mat = if you set t_mat = mat’ if you set t_mat = mat’ t_mat(3, 1) is equal to mat(1,3) Rows and columns are swapped when you transpose a matrix mat’ = 1 2 7 5 9 3

performing operations vect + 3 vect + vect vect - 3 vect + [5, 4, 3] mat+mat’

multiply and divide Don’t forget to put a dot before multiplication and division symbols vect.*3 mat.*0.5 vect./2 vect2 = [2; 2; 4] vect.*vect2 vect./vect2

task 2 Create a 6(rows)x4(columns) matrix filled with random integer values between 1 and 20 Create a zeroes matrix of the same dimensions Replace each zero with a random number from the other matrix (as we did in the mix_strings program) Display each value in the new mixed matrix Hint 1: You will need to use nested loops Hint 2: Look up how to use the “zeros” command ** Advanced task: Design your program so that it can work with matrices of any dimension just by changing the number of rows and column. Hint: Look up the “size” command