Matrices in MATLAB

MATLAB Tutorial

📊 Matrices in MATLAB (With Examples, Output & Interview Questions)

Matrices in MATLAB are two-dimensional arrays and are the core concept of MATLAB (Matrix Laboratory).

MATLAB is developed by MathWorks.


🔹 What Is a Matrix in MATLAB?

A matrix is a collection of elements arranged in rows and columns.

  • Size → m × n (rows × columns)

  • Example: 3 × 3 matrix


1️⃣ Creating Matrices

🔸 Manual Creation


Output

1 2 3
4 5 6
7 8 9

🔸 Using Colon Operator


Output

1 3 5
2 4 6

2️⃣ Matrix Indexing


Output

6

📌 Access element at row 2, column 3.


🔸 Entire Row or Column


 


3️⃣ Matrix Operations

🔸 Matrix Addition


Output

6 8
10 12

🔸 Matrix Multiplication


Output

19 22
43 50

🔸 Element-wise Multiplication


Output

5 12
21 32

4️⃣ Transpose of a Matrix


Output

1 3
2 4

5️⃣ Special Matrices

🔹 Zero Matrix


Output

0 0 0
0 0 0
0 0 0

🔹 Ones Matrix


Output

1 1 1 1
1 1 1 1

🔹 Identity Matrix


Output

1 0 0
0 1 0
0 0 1

6️⃣ Matrix Size & Information


 


7️⃣ Solving Linear Equations


Output

1
2

⚠️ Important Notes

  • Matrix dimensions must be compatible

  • Use * for matrix multiplication

  • Use .* for element-wise multiplication

  • MATLAB indexing starts at 1


🎯 Interview Questions: Matrices in MATLAB

🔹 Q1. What is a matrix in MATLAB?

Answer: A two-dimensional array of elements.


🔹 Q2. Difference between * and .*?

Answer:
* → matrix multiplication
.* → element-wise multiplication


🔹 Q3. How do you access element at row 3, column 2?

Answer: A(3,2)


🔹 Q4. How do you create an identity matrix?

Answer: Using eye().


🔹 Q5. What does A' do?

Answer: Transposes matrix A.


🔹 Q6. How do you solve linear equations in MATLAB?

Answer: Using left division operator \.


🔹 Q7. What is the index of the first row and column?

Answer: 1


Summary

  • Matrices are the foundation of MATLAB

  • Stored in rows & columns

  • Support powerful math operations

  • Essential for engineering & data analysis

You may also like...