MATLAB Examples - Bilkent University

Download Report

Transcript MATLAB Examples - Bilkent University

MATLAB Examples
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 111
2
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 111
3
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 111
4
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 = 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);
CS 111
5
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 ; 1 0 2 ; 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
6
Example: rock-paper-scissors game
% Display user's choice
switch(user)
a=ceil(rand(1)*3);
case{1}
% Get user input
disp('You choose rock');
user=input(' enter 1 for rock
case{2}
\n enter 2 for paper \n enter
disp('You choose paper');
3 for scissors ');
otherwise
% Display your choice
disp('You choose
switch(a)
scissors');
case{1}
end
disp('I choose rock'); win=[0 2 1 ; 1 0 2 ; 2 1 0];
case{2}
result=win(user,a);
% Generate computer’s choice
disp('I choose paper');
case{3}
disp('I choose
scissors');
end
CS 111
% Display result
if result==0
disp('Settle for draw!');
elseif result==1
disp('You win!');
else
disp('You are a loser!');
end
7
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
% 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 ; 1 0 2 ; 2 1 0];
result=win(user,a);
while user~=1 & user~=2 & user~=3
user=input(' enter 1 for rock \n
enter 2 for paper \n enter 3 for
% Display result
scissors ');
if result==0
end
disp('Settle for draw!');
% Display your choice
elseif result==1
switch(a)
disp('You win!');
case{1}
else
disp('I choose rock');
disp('You are a loser!');
case{2}
end
disp('I choose paper');
answer=input('do you want to
case{3}
continue(y,n)? ','s');
end
disp('I choose scissors');
end
8
Example: continue
% 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
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
9
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
end
end
fprintf(‘ii is %d, jj is %d\n',
ii,jj);
CS 111
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
10
ii is 3, jj is 3
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
11
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
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
end
12