Categorical Arrays in MATLAB

MATLAB Tutorial

🏷️ Categorical Arrays in MATLAB

Categorical arrays in MATLAB are designed to store discrete categories (labels) such as gender, class, grade, department, status.

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

Male
Female
Male
Female

🔸 From Cell Array of Char



🔸 Specify Categories Explicitly


📌 Even unused categories are retained.


2️⃣ View Categories


Output

A
B
C
D

3️⃣ Count Each Category


Output

2
1
1
0

4️⃣ Rename Categories



5️⃣ Reorder Categories

Useful for ordered data.



6️⃣ Ordinal (Ordered) Categorical


📌 Enables logical comparisons:


Output

1

7️⃣ Convert Between Types

🔸 String → Categorical


🔸 Categorical → String


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

You may also like...