Matrix Indexing in MATLAB

🔢 Matrix Indexing in MATLAB
MATLAB is developed by MathWorks.
🔹 What Is Matrix Indexing?
Indexing means selecting elements using their row and column positions.
📌 MATLAB uses 1-based indexing (the first element is index 1, not 0).
1️⃣ Basic Matrix Indexing (Row, Column)
🔸 Syntax
🔸 Example
Output
👉 Element at row 2, column 3.
2️⃣ Accessing Entire Rows or Columns
🔸 Entire Row
Output
🔸 Entire Column
Output
📌 Colon : means all rows or all columns.
3️⃣ Accessing Multiple Rows & Columns
Output
👉 Extracts rows 1 to 2 and columns 2 to 3.
4️⃣ Linear Indexing
MATLAB stores matrices column-wise in memory.
Output
📌 Linear indexing counts down columns first.
Order of elements:
5️⃣ Indexing Using End Keyword
Output
📌 end refers to the last row or column.
6️⃣ Logical Indexing
Select elements based on a condition.
Output
📌 Very powerful for filtering data.
7️⃣ Modifying Matrix Elements Using Indexing
Output
8️⃣ Deleting Rows or Columns
🔸 Delete a Row
🔸 Delete a Column
⚠️ Important Notes
Indexing starts from 1
:selects all elements in a dimensionLinear indexing is column-major
Logical indexing is faster and cleaner than loops
🎯 Interview Questions: Matrix Indexing in MATLAB
🔹 Q1. What is matrix indexing?
Answer: Accessing elements of a matrix using row and column indices.
🔹 Q2. What does A(:, :) mean?
Answer: Selects all rows and all columns (entire matrix).
🔹 Q3. What is linear indexing?
Answer: Accessing elements using a single index in column-wise order.
🔹 Q4. How do you access the last element of a matrix?
Answer: A(end, end)
🔹 Q5. What is logical indexing?
Answer: Selecting elements using logical conditions (e.g., A > 10).
🔹 Q6. How do you extract the 2nd row?
Answer: A(2,:)
🔹 Q7. How do you delete a column?
Answer: A(:,column_number) = []
✅ Summary
MATLAB uses 1-based indexing
Supports row, column, linear, and logical indexing
Colon
:andendsimplify selectionEssential for matrix manipulation and data analysis
