📖 Scenario: Imagine you work in a small company that tracks sales data in tables. Each table is a matrix where rows are products and columns are months. You want to do some basic math with these sales tables.
🎯 Goal: You will create two sales matrices, set a multiplier for a sales increase, multiply the matrices, and then print the result.
📋 What You'll Learn
Create two matrices with exact values
Create a multiplier variable
Multiply matrices using matrix multiplication
Print the resulting matrix
💡 Why This Matters
🌍 Real World
Matrix arithmetic is useful in business for analyzing sales data, financial modeling, and scientific calculations.
💼 Career
Many jobs in data analysis, finance, and engineering require working with matrices and performing matrix operations.
Progress0 / 4 steps
1
Create two sales matrices
Create a matrix called sales1 with values 1, 2, 3, 4, 5, 6 arranged in 2 rows and 3 columns, and another matrix called sales2 with values 7, 8, 9, 10, 11, 12 arranged in 3 rows and 2 columns.
R Programming
Hint
Use the matrix() function with c() to combine values, and specify nrow and ncol.
2
Set a sales multiplier
Create a variable called multiplier and set it to 2 to represent doubling sales.
R Programming
Hint
Just assign the number 2 to the variable multiplier.
3
Multiply matrices with multiplier
Create a new matrix called result by multiplying sales1 and sales2 using matrix multiplication, then multiply the result by multiplier.
R Programming
Hint
Use %*% for matrix multiplication and multiply the whole result by multiplier.
4
Print the result matrix
Print the matrix result to display the final sales after multiplication.
R Programming
Hint
Use the print() function to show the matrix result.
Practice
(1/5)
1. In R, what does the operator %*% do when applied between two matrices?
easy
A. Performs matrix multiplication following linear algebra rules
B. Adds the two matrices element-wise
C. Subtracts the second matrix from the first element-wise
D. Multiplies the two matrices element-wise
Solution
Step 1: Understand matrix operators in R
In R, + and - perform element-wise addition and subtraction respectively, while %*% is used for matrix multiplication.
Step 2: Identify the role of %*%
The operator %*% multiplies matrices according to linear algebra rules, not element-wise.
Final Answer:
Performs matrix multiplication following linear algebra rules -> Option A
Quick Check:
%*% = matrix multiplication [OK]
Hint: Remember: %*% means matrix multiplication, not element-wise [OK]
Common Mistakes:
Confusing %*% with element-wise multiplication
Thinking + or - does matrix multiplication
Using * for matrix multiplication
2. Which of the following is the correct way to create a 2x2 matrix with values 1, 2, 3, 4 in R?
easy
A. matrix(1, 2, 3, 4, 2, 2)
B. matrix(1, 2, 3, 4, nrow=2, ncol=2)
C. matrix(c(1, 2, 3, 4), rows=2, cols=2)
D. matrix(c(1, 2, 3, 4), nrow=2, ncol=2)
Solution
Step 1: Recall matrix creation syntax in R
The function matrix() takes a vector of values and parameters nrow and ncol to specify dimensions.
Step 2: Check each option
matrix(c(1, 2, 3, 4), nrow=2, ncol=2) correctly uses c(1, 2, 3, 4) and named arguments nrow=2, ncol=2. matrix(1, 2, 3, 4, nrow=2, ncol=2) has wrong argument format. matrix(c(1, 2, 3, 4), rows=2, cols=2) uses invalid argument names. matrix(1, 2, 3, 4, 2, 2) uses positional arguments without the vector wrapper.
Final Answer:
matrix(c(1, 2, 3, 4), nrow=2, ncol=2) -> Option D
Quick Check:
Use c() vector and nrow, ncol named args [OK]
Hint: Always wrap values in c() and name nrow, ncol [OK]