Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> MATLAB

Mastering MATLAB Vectors: Row and Column Basics

A vector is a one-dimensional array of numbers. MATLAB allows creating two types of vectors −

Row Vectors

Row vectors are created by enclosing the set of elements in square brackets, using space or comma to delimit the elements.

Live Demo
r = [7 8 9 10 11]

MATLAB will execute the above statement and return the following result −

r =

   7    8    9   10   11 

Column Vectors

Column vectors are created by enclosing the set of elements in square brackets, using semicolon to delimit the elements.

Live Demo
c = [7;  8;  9;  10; 11]

MATLAB will execute the above statement and return the following result −

c =
      7       
      8       
      9       
      10       
      11  
 

Referencing the Elements of a Vector

You can reference one or more of the elements of a vector in several ways. The ith component of a vector v is referred as v(i). For example −

Live Demo
v = [ 1; 2; 3; 4; 5; 6];	% creating a column vector of 6 elements
v(3)

MATLAB will execute the above statement and return the following result −

ans =  3

When you reference a vector with a colon, such as v(:), all the components of the vector are listed.

Live Demo
v = [ 1; 2; 3; 4; 5; 6];	% creating a column vector of 6 elements
v(:)

MATLAB will execute the above statement and return the following result −

ans =
     1
     2
     3
     4
     5
     6

MATLAB allows you to select a range of elements from a vector.

For example, let us create a row vector rv of 9 elements, then we will reference the elements 3 to 7 by writing rv(3:7) and create a new vector named sub_rv.

Live Demo
rv = [1 2 3 4 5 6 7 8 9];
sub_rv = rv(3:7)

MATLAB will execute the above statement and return the following result −

sub_rv =

   3   4   5   6   7

Vector Operations

In this section, let us discuss the following vector operations −


MATLAB

  1. Understanding Variables in MATLAB: Basics and Examples
  2. Mastering MATLAB Vectors: Row and Column Basics
  3. Master MATLAB Matrix Basics: Create, Reference, and Manipulate Data
  4. MATLAB – Numbers: Types, Storage, and Conversion
  5. Master MATLAB Strings: Easy Creation & Usage Guide
  6. Plotting Functions in MATLAB: A Step-by-Step Guide
  7. Mastering MATLAB Graphics: Advanced Plotting Techniques
  8. MATLAB for Calculus: Solving Differential Equations, Integrals & Limits
  9. Computing Symbolic Derivatives in MATLAB with the diff Command
  10. Understanding Polynomials in MATLAB: Representation and Evaluation