SIMPLE INPUT and OUTPUT:

Similar documents
The same method can be used for variables that are character strings or other data types.

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

VARIABLES Storing numbers:

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

CONDITIONAL STATEMENTS AND FLOW CONTROL

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

CITS2401 Computer Analysis & Visualisation

Performing Matrix Operations on the TI-83/84

Properties and Definitions

EGR 111 Introduction to MATLAB

MATLAB TUTORIAL WORKSHEET

Computer Vision. Matlab

The continue command goes back to the start of a loop and increments by one unit.

Introduction to MATLAB

APPM 2460 Matlab Basics

Introduction. Matlab for Psychologists. Overview. Coding v. button clicking. Hello, nice to meet you. Variables

MATLAB Fundamentals. Berlin Chen Department of Computer Science & Information Engineering National Taiwan Normal University

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

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

(Refer Slide Time 01:41 min)

Matlab- Command Window Operations, Scalars and Arrays

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

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

MATLAB = MATrix LABoratory. Interactive system. Basic data element is an array that does not require dimensioning.

One-dimensional Array

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

You just told Matlab to create two strings of letters 'I have no idea what I m doing' and to name those strings str1 and str2.

Introduction to MATLAB

Our Strategy for Learning Fortran 90

1 Introduction to Matlab

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

MATLAB GUIDE UMD PHYS401 SPRING 2012

Part II Composition of Functions

Table of Laplace Transforms

SAMLab Tip Sheet #1 Translating Mathematical Formulas Into Excel s Language

Introduction to MATLAB

Experiment 1: Introduction to MATLAB I. Introduction. 1.1 Objectives and Expectations: 1.2 What is MATLAB?

Shorthand for values: variables

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

A Quick Introduction to MATLAB/Octave. Kenny Marino, Nupur Chatterji

rrr Cu 'Vt.-1 cal l-c.-<_ {CL/o,_

Module 201 Object Oriented Programming Lecture 10 - Arrays. Len Shand

Module 6: Array in C

The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development

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

MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras

JavaScript Basics. The Big Picture

Variables, expressions and statements

Dynamics and Vibrations Mupad tutorial

Adding content to your Blackboard 9.1 class

Starting with a great calculator... Variables. Comments. Topic 5: Introduction to Programming in Matlab CSSE, UWA

ENGR 1181 MATLAB 02: Array Creation

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

Section 26: Associativity and Order of Operations

Introduction to MATLAB LAB 1

Digital Image Analysis and Processing CPE

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

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.

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

AMATH 352: MATLAB Tutorial written by Peter Blossey Department of Applied Mathematics University of Washington Seattle, WA

Intermediate Algebra. Gregg Waterman Oregon Institute of Technology

Matlab and Vectors. next page. close. exit. Math 45 Linear Algebra. David Arnold.

ENGR 1181 MATLAB 05: Input and Output

Mathworks (company that releases Matlab ) documentation website is:

McTutorial: A MATLAB Tutorial

Lecture 2. Arrays. 1 Introduction

Decisions, Decisions. Testing, testing C H A P T E R 7

[ the academy_of_code] Senior Beginners

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

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

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

CME 192: Introduction to Matlab

Programming with Python

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Step by step set of instructions to accomplish a task or solve a problem

A/D Converter. Sampling. Figure 1.1: Block Diagram of a DSP System

JME Language Reference Manual

A Brief Introduction to Matlab for Econometrics Simulations. Greg Fischer MIT February 2006

Try typing the following in the Python shell and press return after each calculation. Write the answer the program displays next to the sums below.

Python lab session 1

MATLAB - Lecture # 4

University of Alberta

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

17 USING THE EDITOR AND CREATING PROGRAMS AND FUNCTIONS

Chapter 10 Language Translation

Objectives. 1 Running, and Interface Layout. 2 Toolboxes, Documentation and Tutorials. 3 Basic Calculations. PS 12a Laboratory 1 Spring 2014

Eric W. Hansen. The basic data type is a matrix This is the basic paradigm for computation with MATLAB, and the key to its power. Here s an example:

ECE 3793 Matlab Project 1

1.1 Defining Functions

Chapter 2. MATLAB Fundamentals

First Haskell Exercises

Dr. Nahid Sanzida b e. uet .ac.

The mathematics you have learned about so

Array Creation ENGR 1181 MATLAB 2

Physics 326 Matlab Primer. A Matlab Primer. See the file basics.m, which contains much of the following.

APPM 2460: Week Three For, While and If s

Repetition CSC 121 Fall 2014 Howard Rosenthal

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

These are notes for the third lecture; if statements and loops.

Transcription:

SIMPLE INPUT and OUTPUT: (A) Printing to the screen. The disp( ) command. If you want to print out the values of a variable to the screen, you simply can type the variable at the command line. > x = 5 x = 5 Same thing applies for character strings. > z = hello z = hello You can write character and numerical data on the same line, although it won t print out that way. > 'The value of x is ', x ans = The value of x is x = 5 However, this doesn t look very good. We would like to have more control with the output. Specifically, we would like to get rid of that annoying x =, g =, and ans = after every statement is executed. The disp( ) command is useful if you want to print something to the screen without fancy formatting. > score = 90.2; > disp('your score is') Your score is > disp(score) 90.200 If you want to print different types of data (for example, character and floating-point) on the same line, you must convert the floating-point data to a string using the num2str( ) command. > score = 90.2; > disp(['your test score is ' num2str(score)]) Your test score is 90.2 Notice the use of brackets [ ] around everything you want printed to the screen. 1

(B) Getting input from user. The input( ) function. Sometimes you want a program to read in data provided by the user and then use that data to perform some calculations. For example, the user inputs the number of hours and the program converts that number to minutes and seconds. To read in data from the screen, use the input( ) function. variable or array = input( some instructions for the user ) In mfile.m: hours = input('enter the number of hours: ') minutes = hours * 60.; seconds = minutes * 60.; disp(['this is ' num2str(minutes) ' minutes']) disp(['this is ' num2str(seconds) ' seconds']) % Matlab will wait for a value to be entered At command line: > mfile Enter the number of hours: 5 hours = 5 This is 300 minutes This is 18000 seconds Inputting character data is a little trickier. If you use the same syntax for inputting character data as you use for inputting numerical data, the user must put single quotes around the text they enter. In mfile.m: name = input('what is your name? '); disp(['you have entered: ' name]) At command line: > mfile What is your name? 'Paul' You have entered: Paul (Notice that I had to use single quotes when typing Paul ) If you don t want the user to have to enter character data in single quotes, you can use a slightly different form of the input( ) command. In mfile.m: % The s tells Matlab/Octave that a string of characters is expected name = input('what is your name? ', 's'); disp(['you have entered: ' name]) 2

At command line: > mfile What is your name? Paul You have entered: Paul I will discuss how to enter in multiple pieces of information at once later. ARRAYS: Matlab/Octave is designed for handling Matrices (arrays). You can think of arrays as a series of boxes that contain similar items. We use arrays when we want to store a lot of similar data. For example, if we want to find the mean and standard deviation of student test scores, we first need to store all the scores in memory. It would be quite lengthy and complicated to make a new variable for each test score. Although in theory arrays can have many different dimensions, we usually only deal with 1D (vector) or 2D (Matrix) arrays. For 2D arrays, the size of the array is expresses as #ROWS x #COLUMNS. A 1D array is a 2D array with one row. 1x6 : 3x1: 4x2: 3

An array is made of elements. A 1x6 array is 1-dimensional and has 6 elements. A 3x1 array is 1-dimensional and has 3 elements. A 4x2 array is 2-dimensional and has 8 elements. Extending this concept into 3+ dimensions, a 4x2x3 array has 24 elements and a 2x10x3x2 array has 120 elements. 1D ARRAYS: First we ll discuss 1D arrays since they are simpler to understand than 2D arrays. Every time you create a variable, you create a 1D array that is 1x1 in size. > 52.4 52.400 > whos Variables in the current scope: Attr Name Size Bytes Class ==== ==== ==== ===== ===== a 1x1 8 double Total is 1 element using 8 bytes The variable a is a 1x1 array. Let s create a bigger array. You can define arrays by enclosing numbers in brackets. The value of each element is separated by a comma or a space. > cat = [0.1, 0.2, 0.4] cat = 0.10000 0.20000 0.40000 > dog = [1.1 1.2 2.4] dog = 1.10000 1.20000 2.40000 > whos Variables in the current scope: Attr Name Size Bytes Class ==== ==== ==== ===== ===== cat 1x3 24 double dog 1x3 24 double You have created two 1x3 arrays that contain double precision data. Here are pictures of the cat and dog arrays. You refer to a particular element in the array using parentheses. 4

0.1 0.2 0.4 cat(1) cat(2) cat(3) 1.1 1.2 2.4 dog(1) dog(2) dog(3) > cat(3) ans = 0.40000 > dog(1) ans = 1.10000 > dog(4) error: A(I): Index exceeds matrix dimension. Notice that we get an error message if we try to access a non-existent array element. You can use elements of an array to do mathematical operations. > dog(1) + cat(2) ans = 1.3000 > rat = cat(1)**2 rat = 0.010000 You can change the values stored in an array. > dog(1) = 5 dog = 5.0000 1.2000 2.4000 > dog(1) = dog(3) dog = 2.4000 1.2000 2.4000 We can add elements to arrays very easily. Note: In many other languages (such as Fortran), this is not allowed you would need to declare the size of your array ahead of time. > dog(4) = 10 dog = 2.4000 1.2000 2.4000 10.0000 > whos Variables in the current scope: Attr Name Size Bytes Class ==== ==== ==== ===== ===== cat 1x3 24 double 5

dog 1x4 32 double rat 1x1 8 double The array dog is now a 1x4 array. Continuing from previous line: If you want to define a long 1D array and do not have enough room on the command line, you can use an ellipsis ( ) > cat45 = [1 3 5 6 3 5 3.3... > 55 66 77 888] cat45 = 1.0000 3.0000 5.0000 6.0000 3.0000 5.0000 3.3000 55.0000 66.0000 77.0000 888.0000 2D ARRAYS: 2D arrays are very similar to 1D arrays, except you need to worry about both rows and columns. You separate rows by using a semicolon (;) or hitting the return key. > temp = [2, 4, 5 ; 6, 3, 2] temp = 2 4 5 6 3 2 > temp2 = [2 4 5 (when I hit return, no command is executed) > 6 3 2] temp2 = 2 4 5 6 3 2 (we get the same 2D array using different methods of entering data) > temp(1,2) ans = 4 (4 is now the first row, second column of the array temp) > temp2(2,3) ans = 2 > whos Variables in the current scope: Attr Name Size Bytes Class ==== ==== ==== ===== ===== temp 2x3 48 double temp2 2x3 48 double Both temp and temp2 are 2x3 arrays. As with 1D arrays, we can manipulate specific elements in the 2D arrays. 6

> temp(1,1) = 300 temp = 300 4 5 6 3 2 > temp(2,1) = temp(1,1) temp = 300 4 5 300 3 2 > temp(2,2) * temp(2,3) 6 MATRIX OPERATIONS: There will be times where you will want to perform an operation on all the elements of an array. For example, multiply all the elements of an array by a constant. > [2, 3, 9 ; 4, 4, 7] (a is a 2x3 array) 2 3 9 4 4 7 > d = 3 d = 3 > b = a.*d b = 6 9 27 12 12 21 Notice I used the.* operator instead of just the * operator. Adding a period in front of the operator means do this to every element of the array. When we only are multiplying, dividing, adding, or subtracting by a constant (1x1 array) it doesn t matter if we use the period, but if d was not a constant we may run into trouble. > b = a*d (We get the same result using.* and * in this case) b = 6 9 27 12 12 21 > a + 1 3 4 10 5 5 8 > a.+ 1 (again, we get the same result using.* and * since 1 is a constant) 4 5 11 6 6 9 7

> a/10 0.40000 0.50000 1.10000 0.60000 0.60000 0.90000 However, when we try to raise the array a to the power of 2, we get an error. > a^2 error: for A^b, A must be square This is because Matlab/Octave is trying to multiply the array a by itself (a 2x3 array multiplied by another 2x3 array), which is forbidden by the rules of matrix multiplication. If you want to square each element of the array a, use.^ > a.^2 0.16000 0.25000 1.21000 0.36000 0.36000 0.81000 In general, it is safest to always use a period in front of the operation when you want to perform an operation on each element of an array separately. 8