MATLAB Examples - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

MATLAB Examples

Description:

disp('I choose scissors'); end % Display user's choice. if ... Example: rock-paper-scissors game. answer='y'; while answer=='y' % Generate computer's choice ... – PowerPoint PPT presentation

Number of Views:57
Avg rating:3.0/5.0
Slides: 13
Provided by: selim
Category:

less

Transcript and Presenter's Notes

Title: MATLAB Examples


1
MATLAB Examples
2
MATLAB Examples
  • Find the number of positive numbers in a vector
  • x input( 'Enter a vector ' )count 0for
    ii 1length(x), if ( x(ii) gt 0 ),
    count count 1 endendfprintf('Number of
    positive numbers is d\n', count)

3
MATLAB Examples
  • Find the index of the largest number in a vector
  • x input( 'Enter a vector ' )max_value
    x(1)max_index 1for ii 2length(x), if
    ( x(ii) gt max_value ), max_value
    x(ii) max_index ii
    endendfprintf( 'Max value is d\n', max_value
    )fprintf( 'Its index is d\n', max_index )
  • What if the max value occurs more than once?

4
MATLAB Examples
  • Print a triangle of stars in n rows
  • n input( 'Enter the number of rows ' )for
    ii 1n, for jj 1ii, fprintf( ''
    ) end fprintf( '\n' )end

5
MATLAB Examples
  • Find the kth digit of a number (kth digit from
    the right)
  • num input( 'Enter the number ' )k input(
    'Enter the digit from right you want '
    )num_orig numfor ii 1k-1, num fix(
    num / 10 )enddigit mod( num, 10
    )fprintf('Digit d of d is d\n', k, num_orig,
    digit)

6
Example rock-paper-scissors game
  • Generate computers choice
  • aceil(rand(1)3)
  • Get user input
  • userinput(' enter 1 for rock \n enter 2 for
    paper \n enter 3 for scissors ')
  • Display your choice
  • if a1
  • disp('I choose rock')
  • elseif a2
  • disp('I choose paper')
  • else
  • disp('I choose scissors')
  • end
  • Display user's choice
  • if user1
  • disp('You choose rock')
  • elseif user2
  • disp('You choose paper')
  • else
  • disp('You choose scissors')
  • win0 2 1 1 0 2 2 1 0
  • resultwin(user,a)
  • Display result
  • if result0
  • disp('Settle for draw!')
  • elseif result1
  • disp('You win!')
  • else
  • disp('You are a loser!')
  • end

7
Example rock-paper-scissors game
  • Display user's choice
  • switch(user)
  • case1
  • disp('You choose rock')
  • case2
  • disp('You choose paper')
  • otherwise
  • disp('You choose scissors')
  • end
  • win0 2 1 1 0 2 2 1 0
  • resultwin(user,a)
  • Display result
  • if result0
  • disp('Settle for draw!')
  • elseif result1
  • disp('You win!')
  • else
  • disp('You are a loser!')
  • Generate computers choice
  • aceil(rand(1)3)
  • Get user input
  • userinput(' enter 1 for rock \n enter 2 for
    paper \n enter 3 for scissors ')
  • Display your choice
  • switch(a)
  • case1
  • disp('I choose rock')
  • case2
  • disp('I choose paper')
  • case3
  • disp('I choose scissors')
  • end

8
Example rock-paper-scissors game
  • Display user's choice
  • switch(user)
  • case1
  • disp('You choose rock')
  • case2
  • disp('You choose paper')
  • otherwise
  • disp('You choose scissors')
  • end
  • win0 2 1 1 0 2 2 1 0
  • resultwin(user,a)
  • Display result
  • if result0
  • disp('Settle for draw!')
  • elseif result1
  • disp('You win!')
  • else
  • disp('You are a loser!')
  • end
  • answer'y'
  • while answer'y'
  • Generate computers choice
  • aceil(rand(1)3)
  • Get user input
  • userinput(' enter 1 for rock \n enter 2 for
    paper \n enter 3 for scissors ')
  • Check for erroneous input
  • while user1 user2 user3
  • userinput(' enter 1 for rock \n enter 2 for
    paper \n enter 3 for scissors ')
  • end
  • Display your choice
  • switch(a)
  • case1
  • disp('I choose rock')
  • case2
  • disp('I choose paper')
  • case3
  • disp('I choose scissors')
  • end

9
Example continue
  • Get inputs from the user.
  • arrayinput('Please enter the array to search
    ')
  • ninput('Please enter the number to be searched
    ')
  • Get size of the array.
  • r csize(array)
  • Search for n in the array.
  • for ii1r
  • fprintf('row d\n',ii)
  • for jj1c
  • fprintf(column d\n',jj)
  • if(array(ii,jj)n)
  • fprintf('d found at row d, column
    d\n',n,ii,jj)
  • continue
  • end
  • end
  • end
  • fprintf(ii is d, jj is d\n', ii,jj)
  • Output
  • Please enter the array to search 2 4 5 6 13 2
    5 3 11
  • 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

10
Example break
Output Please enter the array to search 2 4
5 6 13 2 5 3 11 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
  • Get inputs from the user.
  • arrayinput('Please enter the array to search
    ')
  • ninput('Please enter the number to be searched
    ')
  • Get size of the array.
  • r csize(array)
  • Search for n in the array.
  • for ii1r
  • fprintf('row d\n',ii)
  • for jj1c
  • fprintf(column d\n',jj)
  • if(array(ii,jj)n)
  • fprintf('d found at row d, column
    d\n',n,ii,jj)
  • break
  • end
  • end
  • end
  • fprintf(ii is d, jj is d\n', ii,jj)

11
Example student grades
  • Get inputs from user.
  • gradesinput('Enter a grades matrix ')
  • weightsinput('Enter a weights vector ')
  • r csize(grades)
  • multipzeros(r,c)
  • overallzeros(r,1)
  • pass0
  • fail0
  • max1
  • Divide weights by 100 to obtain percent weights
  • weightsweights/100
  • weighttweights'
  • calculate average by matrix multiplication
  • overallgradesweightt
  • print out overall grade
  • for ii1r
  • fprintf('Overall grade of student d
    .2f\n',ii,overall(ii))
  • end

12
Example student grades (contd.)
  • Calculate pass/fail numbers
  • if(overall(ii)gt65)
  • passpass1
  • else
  • failfail1
  • end
  • Highest grade student
  • if(overall(max)ltoverall(ii))
  • maxii
  • end
  • 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 ii1r
Write a Comment
User Comments (0)
About PowerShow.com