Categorical Arrays in MATLAB
🏷️ Categorical Arrays in MATLAB
They are memory-efficient and enable group-wise analysis and plotting.
MATLAB is developed by MathWorks.
🔹 What Is a Categorical Array?
A categorical array:
-
Stores data as labels (categories) instead of raw text
-
Supports ordered/unordered categories
-
Is ideal for grouping, statistics, and plots
📌 Example categories: "Male" / "Female", "Pass" / "Fail", "A / B / C"
1️⃣ Create a Categorical Array
🔸 From Strings
Output
🔸 From Cell Array of Char
🔸 Specify Categories Explicitly
2️⃣ View Categories
3️⃣ Count Each Category
4️⃣ Rename Categories
5️⃣ Reorder Categories
Useful for ordered data.
6️⃣ Ordinal (Ordered) Categorical
📌 Enables logical comparisons:
7️⃣ Convert Between Types
🔸 String → Categorical
8️⃣ Categorical with Tables
Very common in real datasets.
9️⃣ Plotting with Categorical Data
📌 MATLAB automatically groups by category.
⚠️ Important Notes
-
Faster & safer than raw strings for grouping
-
Ideal for tables, statistics, plots
-
Supports missing values:
<undefined> -
Use ordinal only when order matters
🎯 Interview Questions: Categorical Arrays
🔹 Q1. What is a categorical array?
Answer:
A data type for storing discrete labeled categories.
🔹 Q2. Difference between string and categorical?
Answer:
Strings store text; categoricals store labels with grouping logic.
🔹 Q3. How do you count categories?
Answer:
Using countcats().
🔹 Q4. What is an ordinal categorical?
Answer:
A categorical array with a defined order.
🔹 Q5. How do you rename categories?
Answer:
Using renamecats().
🔹 Q6. Where are categorical arrays commonly used?
Answer:
Tables, plots, statistical grouping, real-world datasets.
✅ Summary
-
Categorical arrays handle discrete labels
-
Memory-efficient and analysis-friendly
-
Support ordering, counting, renaming
-
Essential for data science & statistics in MATLAB
