Creating Arrays in MATLAB

MATLAB Tutorial

🧩 Creating Arrays in MATLAB

Arrays in MATLAB are used to store multiple values in a single variable.

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

1 2 3 4

🔸 Column Array



 

Output

1
2
3
4

🔸 2D Array (Matrix)



 

Output

1 2 3
4 5 6

2️⃣ Creating Arrays Using Colon Operator (:)

🔸 Simple Sequence



 

Output

1 2 3 4 5

🔸 With Step Size



 

Output

2 4 6 8 10

3️⃣ Creating Arrays Using Built-in Functions


🔹 zeros() – Zero Array



 

Output

0 0 0 0
0 0 0 0
0 0 0 0

🔹 ones() – Ones Array



 

Output

1 1 1
1 1 1

🔹 eye() – Identity Matrix



 

Output

1 0 0
0 1 0
0 0 1

4️⃣ Creating Arrays Using linspace()

Creates evenly spaced values.



 

Output

1 3.25 5.5 7.75 10

5️⃣ Creating Arrays Using logspace()

Creates logarithmically spaced values.



 

Output

10 100 1000

6️⃣ Creating Multidimensional Arrays



 

Output

(:,:,1)
0 0 0
0 0 0
(:,:,2)
0 0 0
0 0 0

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

You may also like...