📖 Scenario: Imagine you have two lists of numbers representing daily sales and daily costs for a small shop. You want to calculate the profit for each day by subtracting costs from sales, then find the profit margin by dividing profit by sales, and finally calculate the profit growth by squaring the profit values.
🎯 Goal: You will create two vectors for sales and costs, then use element-wise operations .*, ./, and .^ to calculate profit, profit margin, and profit growth for each day.
📋 What You'll Learn
Create two vectors called sales and costs with exact values
Create a variable called days to store the number of days
Calculate profit using element-wise subtraction
Calculate profit_margin using element-wise division
Calculate profit_growth using element-wise power
Display the results using disp
💡 Why This Matters
🌍 Real World
Element-wise operations are useful when you want to perform calculations on each item in a list or array separately, like calculating daily profits or growth rates.
💼 Career
Understanding element-wise operations is important for data analysis, financial modeling, and scientific computing where you work with arrays of data.
Progress0 / 4 steps
1
Create sales and costs vectors
Create a row vector called sales with values [100, 150, 200, 250, 300] and a row vector called costs with values [60, 90, 120, 160, 180].
MATLAB
Hint
Use square brackets [] to create row vectors with commas separating the numbers.
2
Create days variable
Create a variable called days that stores the number of elements in the sales vector using the length function.
MATLAB
Hint
Use length(sales) to find how many numbers are in the sales vector.
3
Calculate profit, profit margin, and profit growth
Calculate profit by subtracting costs from sales using element-wise subtraction. Then calculate profit_margin by dividing profit by sales using element-wise division. Finally, calculate profit_growth by squaring each element of profit using element-wise power.
MATLAB
Hint
Use - for subtraction, ./ for element-wise division, and .^ for element-wise power.
4
Display the results
Use disp to display the variables profit, profit_margin, and profit_growth.
MATLAB
Hint
Use disp('text') to show labels and disp(variable) to show values.
Practice
(1/5)
1. In MATLAB, what does the operator .* do when applied between two arrays of the same size?
easy
A. Performs matrix multiplication of the arrays
B. Performs element-wise multiplication of the arrays
C. Divides the first array by the second array element-wise
D. Raises each element of the first array to the power of the second array
Solution
Step 1: Understand the operator .* in MATLAB
The .* operator multiplies corresponding elements of two arrays of the same size.
Step 2: Differentiate from matrix multiplication
Matrix multiplication uses * without the dot and follows linear algebra rules, unlike element-wise multiplication.
Final Answer:
Performs element-wise multiplication of the arrays -> Option B
Quick Check:
.* means element-wise multiply [OK]
Hint: Dot before operator means element-wise operation [OK]
Common Mistakes:
Confusing .* with matrix multiplication *
Thinking .* divides arrays
Assuming .* raises to power
2. Which of the following is the correct syntax for element-wise division of two arrays A and B in MATLAB?
easy
A. A / B
B. A .\ B
C. A ./ B
D. A // B
Solution
Step 1: Recall element-wise division syntax
Element-wise division in MATLAB uses ./ between arrays.
Step 2: Identify incorrect options
/ is matrix right division, .\ is element-wise left division, and // is not valid in MATLAB.
Final Answer:
A ./ B -> Option C
Quick Check:
./ means element-wise division [OK]
Hint: Use dot before slash for element-wise division [OK]
Common Mistakes:
Using / instead of ./
Confusing left and right division operators
Using invalid operators like //
3. What is the output of the following MATLAB code?
A = [2, 4; 6, 8]; B = [1, 2; 3, 4]; C = A .^ B;
medium
A. [2, 16; 216, 4096]
B. [2, 8; 18, 32]
C. [2, 4; 6, 8]
D. Error due to incompatible sizes
Solution
Step 1: Understand element-wise power operation
The operator .^ raises each element of A to the power of the corresponding element in B.
D. Dimension mismatch error for element-wise multiplication
Solution
Step 1: Check array sizes for element-wise multiplication
X is 1x3 and Y is 1x2, sizes do not match.
Step 2: Understand MATLAB behavior on size mismatch
Element-wise operations require arrays of the same size or compatible broadcasting, which MATLAB does not support here.
Final Answer:
Dimension mismatch error for element-wise multiplication -> Option D
Quick Check:
Different sizes cause error in .* [OK]
Hint: Arrays must be same size for element-wise ops [OK]
Common Mistakes:
Assuming MATLAB auto-broadcasts arrays
Ignoring size mismatch errors
Thinking semicolon affects operation
5. You have two matrices in MATLAB:
M = [1, 2, 3; 4, 5, 6]; N = [2, 0, 1; 3, 4, 5];
You want to create a new matrix R where each element is the result of M element-wise multiplied by N, then divided element-wise by N plus 1. Which MATLAB expression correctly computes R?
hard
A. R = (M .* N) ./ (N + 1);
B. R = M * N ./ (N + 1);
C. R = M .* (N ./ (N + 1));
D. R = (M .* N) / (N + 1);
Solution
Step 1: Identify element-wise multiplication and division
Element-wise multiplication uses .* and element-wise division uses ./.
Step 2: Analyze each option
R = (M .* N) ./ (N + 1); correctly uses .* and ./ with parentheses. R = M * N ./ (N + 1); uses matrix multiplication *, which is incorrect. R = M .* (N ./ (N + 1)); changes the order of operations, not matching the problem. R = (M .* N) / (N + 1); uses matrix division /, which is invalid here.
Final Answer:
R = (M .* N) ./ (N + 1); -> Option A
Quick Check:
Use dot operators for element-wise multiply and divide [OK]
Hint: Use parentheses and dot operators for element-wise math [OK]
Common Mistakes:
Using matrix multiplication instead of element-wise
Forgetting dot before division operator
Misplacing parentheses changing operation order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.