MATLAB Examples. CS 1112 MATLAB Examples Find the number of positive numbers in a vector x = input( 'Enter a vector: ' ); count = 0; for ii = 1:length(x),

Slides:



Advertisements
Similar presentations
Matlab Intro Simple introduction to some basic Matlab syntax. Declaration of a variable [ ] Matrices or vectors Some special (useful) syntax. Control statements.
Advertisements

Standard Algorithms Find the highest number. ! Your name and today’s date ! Find the maximum Dim numbers(20) As Integer.
A MATLAB function is a special type of M-file that runs in its own independent workspace. It receives input data through an input argument list, and returns.
Al-Karma Language School Computer Department Prep. 3.
3-2 What are relational operators and logical values? How to use the input and disp functions. Learn to use if, if-else and else-if conditional statements.
Computer Science 1620 Loops.
Arrays Revisited Selim Aksoy Bilkent University Department of Computer Engineering
Branching and Loops Ayışığı B. Sevdik Bilkent University.
6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
INF 523Q Chapter 3: Program Statements (Examples).
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
Loops For loop for n = [ ] code end While loop while a ~= 3 code end.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Java Programming Practice.
Seek The Multiplication Treasure Copy this problem. Multiply the factors in the top row by the digit in the ones place in the second factor.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
CPSC 171 Introduction to Computer Science 3 Levels of Understanding Algorithms More Algorithm Discovery and Design.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
ARRAYS 1.Basic Ideas 2.The Array Type 3.Processing Arrays 4.Parallel Arrays 5.Two-dimensional Array 6.Arrays as Parameters.
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.
1 Flow of control Sequential Executing instructions one by one, in exact order given Selection Choosing to execute a particular set of statements depending.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
September 13, 2005 Lecture 4 - By P. Lin 1 CPET 190 Lecture 4 Problem Solving with MATLAB
Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
ENG College of Engineering Engineering Education Innovation Center 1 More Script Files in MATLAB Script File I/O : Chapter 4 1.Global Variables.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
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.
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
1 CS1371 Introduction to Computing for Engineers Control Statements 9/4/2003.
Using GA’s X 3 – y 2 + Z = Create a random population Function name – population Inputs: – N, size of the population – R, range of values for each.
Arrays. Arrays are objects that help us organize large amounts of information.
Lesson 5-4 Example Example 1 Draw an array to model and find 21 ÷ 3. 1.Write the answer if you know it. Otherwise, draw an array.
PATTERN RECOGNITION LAB 2 TA : Nouf Al-Harbi::
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Design of Rock-Paper-Scissors end Print instructions Play game Ask user If (s)he wants to play again Play again? Y Print stats: wins, losses and ties start.
Chapter 4 MATLAB Programming
CS1371 Introduction to Computing for Engineers
Chapter 4: Control Structures
“Clumsy mixture of ingredients…”
Arrays, Part 1 of 2 Topics Definition of a Data Structure
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Loop Statements & Vectorizing Code
Arrays Topics Definition of a Data Structure Definition of an Array
Big problem  small steps
Arrays, Part 1 of 2 Topics Definition of a Data Structure
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.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays Topics Definition of a Data Structure Definition of an Array
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
ME 123 Computer Applications I Lecture 5: Input and Output 3/17/03
Presentation transcript:

MATLAB Examples

CS 1112 MATLAB Examples Find the number of positive numbers in a vector x = input( 'Enter a vector: ' ); count = 0; for ii = 1:length(x), if ( x(ii) > 0 ), count = count + 1; end end fprintf('Number of positive numbers is %d\n', count);

CS 1113 MATLAB Examples Find the index of the largest number in a vector x = input( 'Enter a vector: ' ); max_value = x(1); max_index = 1; for ii = 2:length(x), if ( x(ii) > max_value ), max_value = x(ii); max_index = ii; end end fprintf( 'Max value is %d\n', max_value ); fprintf( 'Its index is %d\n', max_index ); What if the max value occurs more than once?

CS 1114 MATLAB Examples Print a triangle of stars in n rows n = input( 'Enter the number of rows: ' ); for ii = 1:n, for jj = 1:ii, fprintf( '*' ); end fprintf( '\n' ); end

CS 1115 MATLAB Examples Find the k th digit of a number (k th digit from the right) num = input( 'Enter the number: ' ); k = input( 'Enter the digit from right you want: ' ); num_orig = num; for ii = 1:k-1, num = fix( num / 10 ); end digit = mod( num, 10 ); fprintf('Digit %d of %d is %d\n', k, num_orig, digit);

6 Example: rock-paper-scissors game % Generate computer’s choice a=ceil(rand(1)*3); % Get user input user=input(' enter 1 for rock \n enter 2 for paper \n enter 3 for scissors '); % Display your choice if a==1; disp('I choose rock'); elseif a==2; disp('I choose paper'); else disp('I choose scissors'); end % Display user's choice if user==1; disp('You choose rock'); elseif user==2; disp('You choose paper'); else disp('You choose scissors'); end win=[0 2 1 ; ; 2 1 0]; result=win(user,a); % Display result if result==0 disp('Settle for draw!'); elseif result==1 disp('You win!'); else disp('You are a loser!'); end

CS 1117 Example: rock-paper-scissors game % Generate computer’s choice a=ceil(rand(1)*3); % Get user input user=input(' enter 1 for rock \n enter 2 for paper \n enter 3 for scissors '); % Display your choice switch(a) case{1} disp('I choose rock'); case{2} disp('I choose paper'); case{3} disp('I choose scissors'); end % Display user's choice switch(user) case{1} disp('You choose rock'); case{2} disp('You choose paper'); otherwise disp('You choose scissors'); end win=[0 2 1 ; ; 2 1 0]; result=win(user,a); % Display result if result==0 disp('Settle for draw!'); elseif result==1 disp('You win!'); else disp('You are a loser!'); end

8 Example: rock-paper-scissors game answer='y'; while answer=='y' % Generate computer’s choice a=ceil(rand(1)*3); % Get user input user=input(' enter 1 for rock \n enter 2 for paper \n enter 3 for scissors '); % Check for erroneous input while user~=1 & user~=2 & user~=3 user=input(' enter 1 for rock \n enter 2 for paper \n enter 3 for scissors '); end % Display your choice switch(a) case{1} disp('I choose rock'); case{2} disp('I choose paper'); case{3} disp('I choose scissors'); end % Display user's choice switch(user) case{1} disp('You choose rock'); case{2} disp('You choose paper'); otherwise disp('You choose scissors'); end win=[0 2 1 ; ; 2 1 0]; result=win(user,a); % Display result if result==0 disp('Settle for draw!'); elseif result==1 disp('You win!'); else disp('You are a loser!'); end answer=input('do you want to continue(y,n)? ','s'); end

9 Example: continue Output: Please enter the array to search :[2 4 5; ; ] Please enter the number to be searched :13 row 1 column 1 column 2 column 3 row 2 column 1 column 2 13 found at row 2, column 2 column 3 row 3 column 1 column 2 column 3 ii is 3, jj is 3 % Get inputs from the user. array=input('Please enter the array to search :'); n=input('Please enter the number to be searched :'); % Get size of the array. [r c]=size(array); % Search for n in the array. for ii=1:r fprintf('row %d\n',ii); for jj=1:c fprintf(‘column %d\n',jj); if(array(ii,jj)==n) fprintf('%d found at row %d, column %d\n',n,ii,jj); continue; end fprintf(‘ii is %d, jj is %d\n', ii,jj);

CS Example: break % Get inputs from the user. array=input('Please enter the array to search :'); n=input('Please enter the number to be searched :'); % Get size of the array. [r c]=size(array); % Search for n in the array. for ii=1:r fprintf('row %d\n',ii); for jj=1:c fprintf(‘column %d\n',jj); if(array(ii,jj)==n) fprintf('%d found at row %d, column %d\n',n,ii,jj); break; end fprintf(‘ii is %d, jj is %d\n', ii,jj); Output: Please enter the array to search :[2 4 5; ; ] Please enter the number to be searched :13 row 1 column 1 column 2 column 3 row 2 column 1 column 2 13 found at row 2, column 2 row 3 column 1 column 2 column 3 ii is 3, jj is 3

11 Example: student grades % Get inputs from user. grades=input('Enter a grades matrix: '); weights=input('Enter a weights vector: '); [r c]=size(grades); multip=zeros(r,c); overall=zeros(r,1); pass=0; fail=0; max=1; % Divide weights by 100 to obtain percent weights weights=weights/100; weightt=weights' % calculate average by matrix multiplication overall=grades*weightt; %print out overall grade for ii=1:r fprintf('Overall grade of student %d: %.2f\n',ii,overall(ii)); end

12 Example: student grades (contd.) % Calculate pass/fail numbers if(overall(ii)>=65) pass=pass+1; else fail=fail+1; end % Highest grade student if(overall(max)<overall(ii)) max=ii; end % Print out number of passing / failing students fprintf('The number of passing students is: %d\n',pass); fprintf('The number of failing students is: %d\n',fail); % Print out who got the highest overall grade. for ii=1:r if(overall(ii)==overall(max)) fprintf('Student %d got the highest overall grade.\n',ii); end