Creating Arrays in MATLAB
🧩 Creating Arrays in MATLAB
MATLAB supports 1D (vectors), 2D (matrices), and multidimensional arrays.
MATLAB is developed by MathWorks.
🔹 What Is an Array in MATLAB?
An array is a collection of elements stored in rows and columns.
-
1D → Vector
-
2D → Matrix
-
3D+ → Multidimensional array
1️⃣ Creating Arrays Manually
🔸 Row Array
Output
🔸 Column Array
Output
🔸 2D Array (Matrix)
Output
2️⃣ Creating Arrays Using Colon Operator (:)
🔸 Simple Sequence
Output
🔸 With Step Size
Output
3️⃣ Creating Arrays Using Built-in Functions
🔹 zeros() – Zero Array
Output
🔹 ones() – Ones Array
Output
🔹 eye() – Identity Matrix
Output
4️⃣ Creating Arrays Using linspace()
Creates evenly spaced values.
Output
5️⃣ Creating Arrays Using logspace()
Creates logarithmically spaced values.
Output
6️⃣ Creating Multidimensional Arrays
Output
7️⃣ Checking Array Size & Type
⚠️ Important Notes
-
Elements in a row are separated by spaces or commas
-
Rows are separated by semicolons
-
MATLAB indexing starts from 1
-
Default numeric array type is double
🎯 Interview Questions: Creating Arrays in MATLAB
🔹 Q1. What is an array in MATLAB?
Answer: A collection of values stored in rows and columns.
🔹 Q2. How do you create a row and column array?
Answer:
Row → [1 2 3]
Column → [1; 2; 3]
🔹 Q3. Which function creates a zero array?
Answer: zeros()
🔹 Q4. Difference between linspace() and : operator?
Answer:linspace() specifies number of points, : specifies step size.
🔹 Q5. How do you create an identity matrix?
Answer: eye(n)
🔹 Q6. What is the default data type of arrays in MATLAB?
Answer: double
✅ Summary
-
Arrays store multiple values
-
Created manually or using functions
-
Support 1D, 2D, and multi-dimensional forms
-
Core concept of MATLAB programming
