Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

ENG10004 – Digital and Data Systems Swinburne University of Technology, S2 2021

ENG10004 – Digital and Data Systems


Matlab Tutorial 5 – Matlab - Examples & Applications
➢ Generating a Truth Table using Matlab Logic Operators
Suppose we have the Boolean Expression:
E = 𝐴 ∙ 𝐵̅ + 𝐶 ∙ 𝐷
̅
Four inputs: A,B,C,D 𝟐𝟒 combinations

Logic Syntax Description


Functions
and A&B Performs a logical AND of arrays A and B and returns an array
and(A,B) containing elements set to either logical 1 (true) or logical 0
(false)
or A|B Performs a logical OR of arrays A and B and returns an array
or(A,B) containing elements set to either logical 1 (true) or logical 0
(false)
not ~A Returns a logical array which contains logical 1 (true) values
not(A) where A is zero and logical 0 (false) values where A is nonzero
xor C = xor(A,B) Performs a logical exclusive-OR of arrays A and B and returns
an array containing elements set to either logical 1 (true) or
logical 0 (false)

Page 1 of 5
ENG10004 – Digital and Data Systems Swinburne University of Technology, S2 2021

Using and, or, not functions


A=[0 1];B=[0 1];C=[0 1];D=[0 1];
disp('----------------------')
disp('A B C D E ')
disp('----------------------')
for a=1:2
for b=1:2
for c=1:2
for d=1:2
% 2 logic-NOT gates and 2 logic-AND gates
and1 = and(A(a), not(B(b)));
and2 = and(C(c), not(D(d)));
% 1 logic-OR gate
E = or(and1, and2);
fprintf('%d %d %d %d %d\n', A(a),B(b),C(c),D(d),E);
end
end
end
end
Note: and(,), or(,), xor(,): two inputs; not(): one input!

Using &, |, ~ operators


A=[0 1];B=[0 1];C=[0 1];D=[0 1];
disp('----------------------')
disp('A B C D E ')
disp('----------------------')
for a=1:2
for b=1:2
for c=1:2
for d=1:2
% 2 logic-NOT gates and 2 logic-AND gates
and1 = A(a) & ~B(b);
and2 = C(c) & ~D(d);
% 1 logic-OR gate
E = and1 | and2;
fprintf('%d %d %d %d %d\n', A(a),B(b),C(c),D(d),E);
end
end
end
end

Page 2 of 5
ENG10004 – Digital and Data Systems Swinburne University of Technology, S2 2021

➢ Matlab Programming Example


A Simple Rock-Paper-Scissors Program
Problem: Write an MATLAB program to simulate a rock-paper-scissors game between a person
and a computer with the following requirements:

• The person can input “Rock” or “Paper” or “Scissors” in Command Window or exit the game;

• The program can show the choices of the person and computer as well as the score in each
game; and

• The game continues unless the person quits.

Page 3 of 5
ENG10004 – Digital and Data Systems Swinburne University of Technology, S2 2021

Brainstorming:
Program Flowchart

• The problem can be solved by using a while loop since the number of games is not known
before the loop starts;

• Each game result can be determined by a user-defined function combining keyboard input
and random computer choice;

• Rock id: 1; Paper id: 2; Scissors id: 3

Page 4 of 5
ENG10004 – Digital and Data Systems Swinburne University of Technology, S2 2021

Solution: Main m-file script:


clear all; close all; clc;
global win_id tie_id lose_id
win_id = 1; tie_id = 2; lose_id = 3;
wintext = char('You win!', 'You tie!', 'You lose!');
itemtext = char('Rock', 'Paper', 'Scissors');
scores = [0 0 0]; % store score for win, tie, lose

fprintf('The Game starts.\n Enter R for Rock; P for Paper; S for Scissors; Q for
Quit.\n')
while (1)
user_in = input('Please enter R, P, S, or Q: ','s');
user_id = strfind('RPSQ',user_in); % returns the starting index of user_in in string
RPSQ
if (isempty(user_id) || user_id<1 || user_id>4)
fprintf('>>>>Invalid input! Try Again!<<<<\n')
pause(1.5)
elseif (user_id==4)
fprintf('<<You quit the Game. Bye!>>\n')
break;
else
computer_id = randi(3,1);
iresult = rps_game(user_id,computer_id);
fprintf(' You chose -> %s\n Computer choses -> %s\n So -> %s\n', ...
itemtext(user_id,:),itemtext(computer_id,:), ...
wintext(iresult,:));
scores(iresult) = scores(iresult)+1;
fprintf('Your Score:\n Games: %i, Wins: %i, Ties: %i, Losses: %i\n\n', ...
sum(scores),scores);
end
end

rps_game function:
function f = rps_game(user_id,computer_id)
global win_id tie_id lose_id
table = [tie_id lose_id win_id;
win_id tie_id lose_id;
lose_id win_id tie_id];
f = table(user_id,computer_id);
end

Key Points:

• char function to create an array of characters (same length);


• while-loop;
• global variables;
• break a loop;
• user-defined function;
• fprintf function to display characters and integers.

Page 5 of 5

You might also like