Dec 04, 2025 | 702 words | 7 min read
15.2.2. Task 2#
Learning Objectives#
Practice using simple logical operators in MATLAB. Practice manipulating simple vectors and matrices in MATLAB. Understand the purpose of nested conditional statements and loop structures in MATLAB.
Introduction#
This task is designed to introduce you to the MATLAB IDE and syntax. You will be asked to explore various operations in MATLAB and determine the differences from Python. Although the syntax differs, the coding applications are the same as what you have previously seen in Python. You will also be asked to explore common built-in functions in MATLAB. These will be useful for future assignments, so make sure that you understand how each function works. Lastly, you will be getting practice with vector and matrix operations. MATLAB is designed to handle vectors and matrices via indexing and mathematical operations. This is a fundamental part of MATLAB, and it will help you with tasks in the remaining part of the semester.
Task Instructions#
Part A: Logical Operators#
Open up MATLAB and type
editin the Command Window. Then save your file as ma1_team_2_a_teamnumber.m. Make sure to use the MATLAB Template (ENGR133_MATLAB_Template.m).In the
INTIALIZATIONsection, create the following vectors and arrays:Cvector = [1, 2];Aarray = [1, 2; 3, 4];Barray = [1, 0; -1, 4];Carray = [1, 2; 3, 4; 5, 6];Darray = [0, 0, 1; 3, 5, -5; 1, 0, 1];
Open up the Answer Sheet (
MA1_Team_2_teamnumber.docx). Then compute each of the operations under the%% CALCULATIONSsection of your script.Note
If any calculations cause errors, comment them out and re-run the code.
Fill in the table with the calculated output and an explanation of what MATLAB did to get the result or why MATLAB cannot perform the operation.
Ans_A = Aarray >= BarrayAns_B = Aarray .* Barray ~= 1Ans_C = Barray < (Aarray – Ans_B) <= (Ans_B < 1) * 3Ans_D = Barray > CarrayAns_E = [Barray; Cvector] == Carray
Fill in the table with the calculated output and an explanation of what MATLAB did to get the result or why MATLAB cannot perform the operation. Use MATLAB documentation to learn about
any,all, andfindfunctions.Ans_F = any(Aarray) + any(Barray)Ans_G = all(Aarray) + all(Barray)Ans_H = all(Carray > 1)Ans_I = all(any(Barray < -1))+any(all(Darray))Ans_J = find(Barray).^(find(Carray > 5))Ans_K = find(any(Darray == 1))
Part B: Vector Manipulation#
Open up MATLAB and type
editin the Command Window. Then save your file as ma1_team_2_b_teamnumber.m. Make sure to use the MATLAB Template (ENGR133_MATLAB_Template.m).In
%% INTIALIZATION, create the following scalars and vectors:Ascalar = 3Arowvector = [0 1 2 3]Browvector = 4:-2:-2Note
This notation
start:step:endallows you to create vectors of any size with any starting value, increment step size, and ending value. This example creates a four element vector starting at \(4\) and ending at \(-2\) with increments of \(-2\).Crowvectorwith three elements of your choice between \(-5\) and \(5\) inclusive.Acolvector = [0; 1; 2; 3]Note
This is a \(4\times1\) column vector, which is different from Arowvector. You could also create Acolvector by computing the transpose of Arowvector by typing Acolvector = Arowvector’
Bcolvector = [-4; -3; -2; -1]
Open the Answer Sheet from part A. Then compute each of the operations under the
CALCULATIONSsection of your script.Note
If any calculations cause errors, comment them out and re-run the code.
Fill in the table with the calculated output and an explanation of what MATLAB did to get the result or why MATLAB cannot perform the operation.
Calc1 = Arowvector + ArowvectorCalc2 = Arowvector + BrowvectorCalc3 = Arowvector + AscalarCalc4 = Arowvector - ArowvectorCalc5 = Arowvector - CrowvectorCalc6 = Acolvector + BcolvectorCalc7 = Arowvector + Bcolvector
Fill in the table with the calculated output and an explanation of what MATLAB did to get the result or why MATLAB cannot perform the operation.
Calc8 = Arowvector * BrowvectorCalc9 = Arowvector .* BrowvectorCalc10 = Arowvector * AscalarCalc11 = Arowvector .* AscalarCalc12 = Arowvector ./ BrowvectorCalc13 = Arowvector ^ AscalarCalc14 = Arowvector .^ Ascalar
Save the Answer Sheet as ma1_team_2_teamnumber.pdf.
Part C: Nested Conditionals#
Open up MATLAB and type edit in the Command Window.
Then save your file as
ma1_team_2_c_teamnumber.m. Make sure to
use the MATLAB Template
(ENGR133_MATLAB_Template.m).
Below is a set of nested conditional statements written in Python syntax. The
goal of this part of the task is to rewrite the set of nested conditional statements in
MATLAB. Publish your script as
ma1_team_2_c_pub_teamnumber.pdf with
x and y hardcoded as x = 40 and y = 40.
x = int(input("Choose x value: "))
y = int(input("Choose y value: "))
if x <= 50:
z = 4
if y < 30:
z = x * y
elif y >= 100:
z = x + y
elif y >= 60:
if x > 80:
z = x
elif y > 50:
z = y
else:
z = z * 2
print(f"z = {z}")
Hint
Remember to add end at the end of each if statement and loop!
Sample Output#
Use the values in Table 15.3 below to test your program.
Case |
x |
y |
|---|---|---|
1 |
40 |
40 |
2 |
20 |
20 |
3 |
55 |
22 |
Ensure your program’s output matches the provided samples exactly. This includes all characters, white space, and punctuation. In the samples, user input is highlighted like this for clarity, but your program should not highlight user input in this way.
Case 1 Sample Output
>> ma1_team_2_c_teamnumber Choose x value: 40 Choose y value: 40 z = 4
Case 2 Sample Output
>> ma1_team_2_c_teamnumber Choose x value: 20 Choose y value: 20 z = 400
Case 3 Sample Output
>> ma1_team_2_c_teamnumber Choose x value: 55 Choose y value: 22 {Unrecognized function or variable 'z'.
Error in solution (line 56) z = z * 2; ^ }
Part D: Loop Structures#
Open up MATLAB and type edit in the Command Window.
Then save your file as
ma1_team_2_d_teamnumber.m. Make sure to
use the MATLAB Template
(ENGR133_MATLAB_Template.m).
Below is a set of loop structures written in Python syntax. The goal of this part of this task is to rewrite the set of loop structures in MATLAB. Publish your script as ma1_team_2_d_pub_teamnumber.pdf. Use the same variable names used in the script.
i = 1
x = 3
y = 6
print("While Loop Ouput:")
while i < 10:
for z in [x, y]:
i += z
if x < 3:
x += 1
print(i)
w = [3, 6, -2, 1]
u = 0
print("For Loop Output:")
for index in w:
if index <= 3:
u = u + index
print(f"u = {u}")
Sample Output#
Sample Output
>> ma1_team_2_d_teamnumber While Loop Output: 10 For Loop Output: u = 3 u = 3 u = 1 u = 2
Deliverables |
Description |
|---|---|
ma1_team_2_a_teamnumber.m |
Your completed MATLAB code for part A. |
ma1_team_2_b_teamnumber.m |
Your completed MATLAB code for part B. |
ma1_team_2_c_teamnumber.m |
Your completed MATLAB code for part C. |
ma1_team_2_d_teamnumber.m |
Your completed MATLAB code for part D. |
ma1_team_2_c_pub_teamnumber.pdf |
PDF with published MATLAB code for part C. |
ma1_team_2_d_pub_teamnumber.pdf |
PDF with published MATLAB code for part D. |
ma1_team_2_teamnumber.pdf |
Your answers for each part of this task. |