Vectors in MATLAB

MATLAB Tutorial

📐 Vectors in MATLAB

Vectors in MATLAB are one-dimensional arrays used to store and manipulate a list of numbers efficiently.

MATLAB is developed by MathWorks.


🔹 What Is a Vector in MATLAB?

A vector is a collection of elements arranged in one row or one column.

  • Row Vector → 1 × n

  • Column Vector → n × 1


1️⃣ Creating Vectors

🔸 Row Vector


Output

1 2 3 4 5

🔸 Column Vector


Output

1
2
3
4
5

🔸 Using Colon Operator


Output

1 2 3 4 5

🔸 With Step Size


Output

1 3 5 7 9

2️⃣ Vector Indexing & Accessing Elements


Output

30

📌 Indexing in MATLAB starts from 1.


3️⃣ Vector Operations

🔸 Addition & Subtraction


Output

5 7 9

🔸 Element-wise Multiplication


Output

4 10 18

🔸 Scalar Operation


Output

2 4 6

4️⃣ Common Vector Functions

 Length of Vector

length(v)

 Sum & Mean

sum(v)
mean(v)

 Maximum & Minimum

max(v)
min(v)

5️⃣ Transpose of a Vector


Output

1
2
3

6️⃣ Logical Operations on Vectors


Output

0 0 1 1

⚠️ Important Notes

  • Vectors must be of same size for element-wise operations

  • Use .*, ./, .^ for element-wise math

  • MATLAB indexing starts at 1


🎯 Interview Questions: Vectors in MATLAB

 Q1. What is a vector in MATLAB?

Answer: A one-dimensional array of elements.


 Q2. Difference between row and column vector?

Answer:
Row vector → 1×n, Column vector → n×1.


 Q3. How do you create a vector from 1 to 10?

Answer: v = 1:10;


 Q4. How do you access the 4th element of a vector v?

Answer: v(4)


 Q5. What operator is used for element-wise multiplication?

Answer: .*


 Q6. How do you find the length of a vector?

Answer: Using length().


 Q7. What is the index of the first element in MATLAB?

Answer: 1


Summary

  • Vectors are 1D arrays

  • Can be row or column

  • Support fast mathematical operations

  • Widely used in engineering & data analysis

You may also like...