MATLAB Programming/Arrays/Basic vector operations - Wikibooks, open books for an open world (2024)

[MATLAB Programming\|/MATLAB Programming]m]

Chapter 1: MATLAB ._.

 Introductions .
Fundamentals of MATLAB
MATLAB Workspace
MATLAB Variables
*.mat files

Chapter 2: MATLAB Concepts

MATLAB operator
Data File I/O

Chapter 3: Variable Manipulation

Numbers and Booleans
Strings
Portable Functions
Complex Numbers

Chapter 4: Vector and matrices

Vector and Matrices
Special Matrices
Operation on Vectors
Operation on Matrices
Sparse Matrices

Chapter 5: Array

Arrays
Introduction to array operations
Vectors and Basic Vector Operations
Mathematics with Vectors and Matrices
Struct Arrays
Cell Arrays

Chapter 6: Graphical Plotting

Basic Graphics Commands
Plot
Polar Plot
Semilogx or Semilogy
Loglog
Bode Plot
Nichols Plot
Nyquist Plot

Chapter 7: M File Programming

Scripts
Comments
The Input Function
Control Flow
Loops and Branches
Error Messages
Debugging M Files

Chapter 8: Advanced Topics

Numerical Manipulation
Advanced File I/O
Object Oriented Programming
Applications and Examples
Toolboxes and Extensions

Chapter 9: Bonus chapters

MATLAB Benefits and Caveats
Alternatives to MATLAB
[MATLAB_Programming/GNU_Octave|What is Octave= (8) hsrmonic functions]
Octave/MATLAB differences

edit this box

A vector in MATLAB is defined as an array which has only one dimension with a size greater than one. For example, the array [1,2,3] counts as a vector. There are several operations you can perform with vectors which don't make a lot of sense with other arrays such as matrices. However, since a vector is a special case of a matrix, any matrix functions can also be performed on vectors as well provided that the operation makes sense mathematically (for instance, you can matrix-multiply a vertical and a horizontal vector). This section focuses on the operations that can only be performed with vectors.

Contents

  • 1 Declaring a vector
    • 1.1 Declaring a vector with linear or logarithmic spacing
  • 2 Vector Magnitude
  • 3 Dot product
  • 4 Cross Product

Declaring a vector

[edit | edit source]

Declare vectors as if they were normal arrays, all dimensions except for one must have length 1. It does not matter if the array is vertical or horizontal. For instance, both of the following are vectors:

>> Horiz = [1,2,3];>> Vert = [4;5;6];

You can use the isvector function to determine in the midst of a program if a variable is a vector or not before attempting to use it for a vector operation. This is useful for error checking.

>> isvector(Horiz)ans = 1>> isvector(Vert)ans = 1

Another way to create a vector is to assign a single row or column of a matrix to another variable:

>> A = [1,2,3;4,5,6];>> Vec = A(1,:)Vec = 1 2 3

This is a useful way to store multiple vectors and then extract them when you need to use them. For example, gradients can be stored in the form of the Jacobian (which is how the symbolic math toolbox will return the derivative of a multiple variable function) and extracted as needed to find the magnitude of the derivative of a specific function in a system.

Declaring a vector with linear or logarithmic spacing

[edit | edit source]

Suppose you wish to declare a vector which varies linearly between two endpoints. For example, the vector [1,2,3] varies linearly between 1 and 3, and the vector [1,1.1,1.2,1.3,...,2.9,3] also varies linearly between 1 and 3. To avoid having to type out all those terms, MATLAB comes with a convenient function called linspace to declare such vectors automatically:

>> LinVector = linspace(1,3,21) LinVector = Columns 1 through 9 1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000 Columns 10 through 18 1.9000 2.0000 2.1000 2.2000 2.3000 2.4000 2.5000 2.6000 2.7000 Columns 19 through 21 2.8000 2.9000 3.0000

Note that linspace produces a row vector, not a column vector. To get a column vector use the transpose operator (') on LinVector.

The third argument to the function is the total size of the vector you want, which will include the first two arguments as endpoints and n - 2 other points in between. If you omit the third argument, MATLAB assumes you want the array to have 100 elements.

If, instead, you want the spacing to be logarithmic, use the logspace function. This function, unlike the linspace function, does not find n - 2 points between the first two arguments a and b. Instead it finds n-2 points between 10^a and 10^b as follows:

>> LogVector = logspace(1,3,21) LogVector = 1.0e+003 * Columns 1 through 9 0.0100 0.0126 0.0158 0.0200 0.0251 0.0316 0.0398 0.0501 0.0631 Columns 10 through 18 0.0794 0.1000 0.1259 0.1585 0.1995 0.2512 0.3162 0.3981 0.5012 Columns 19 through 21 0.6310 0.7943 1.0000

Both of these functions are useful for generating points that you wish to evaluate another function at, for plotting purposes on rectangular and logarithmic axes respectively.

Vector Magnitude

[edit | edit source]

The magnitude of a vector can be found using the norm function:

>> Magnitude = norm(inputvector,2);

For example:

>> magHoriz = norm(Horiz) magHoriz = 3.7417>> magVert = norm(Vert)magVert = 8.7750

The input vector can be either horizontal or vertical.

Dot product

[edit | edit source]

The dot product of two vectors of the same size (vertical or horizontal, it doesn't matter as long as the long axis is the same length) is found using the dot function as follows:

>> DP = dot(Horiz, Vert)DP = 32

The dot product produces a scalar value, which can be used to find the angle if used in combination with the magnitudes of the two vectors as follows:

>> theta = acos(DP/(magHoriz*magVert));>> theta = 0.2257

Note that this angle is in radians, not degrees.

Cross Product

[edit | edit source]

The cross product of two vectors of size 3 is computed using the 'cross' function:

>> CP = cross(Horiz, Vert)CP = -3 6 -3

Note that the cross product is a vector. Analogous to the dot product, the angle between two vectors can also be found using the cross product's magnitude:

>> CPMag = norm(CP);>> theta = asin(CPMag/(magHoriz*magVert))theta = 0.2257

The cross product itself is always perpendicular to both of the two initial vectors. If the cross product is zero then the two original vectors were parallel to each other.

MATLAB Programming/Arrays/Basic vector operations - Wikibooks, open books for an open world (2024)

FAQs

What is a vector array in MATLAB? ›

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

How to do vector operation in MATLAB? ›

Vector Operators
  1. Division. Dividing every element by a single value is accomplished just using the / for division. ...
  2. Multiplication. Multiplying every element by a single value is accomplished just using the *. ...
  3. Other Operations. Power (^) and other operators generally work in a similar method.

What is the difference between an array and a matrix in MATLAB? ›

The ismatrix documentation states that a matrix "A matrix is a two-dimensional array that has a size of m-by-n, where m and n are nonnegative integers." Arrays have any number of dimensions, as far as I am concerned an array does not need to have pages, it can also be 2D (i.e. matrix) or scalar or empty.

What are array operations in MATLAB? ›

Array Operations. Array operations execute element by element operations on corresponding elements of vectors, matrices, and multidimensional arrays. If the operands have the same size, then each element in the first operand gets matched up with the element in the same location in the second operand.

Why is vector better than array? ›

Unlike arrays, Vectors can grow or shrink dynamically, accommodating more elements than were initially declared or reducing the storage used based on the elements it currently holds.

What is the difference between array and vector array? ›

A vector is a dynamically allocated, variable-sized collection. An array is a “compile-time” allocated, fixed-size collection. “Compile-time” allocated refers to how the size of the array must be known at compile time, the actual allocation obviously happens when the array is created.

What are examples of vectors in MATLAB? ›

In MATLAB a vector is a matrix with either one row or one column. In two dimensional system, a vector is usually represented by 1 × 2 matrix. For example, a vector, B in Figure 1 is 6i + 3j, where i and j are unit vectors in the positive direction for x and y axes, respectively in the Cartesian coordinate system.

How to build a vector in MATLAB? ›

You can create a vector both by enclosing the elements in square brackets like v=[1 2 3 4 5] or using commas, like v=[1,2,3,4,5].

How to write a vector function in MATLAB? ›

Vector Functions
  1. >> v = [1 2 3]' v = 1 2 3 >> b = [2 4 6]' b = 2 4 6 >> v+b ans = 3 6 9 >> v-b ans = -1 -2 -3.
  2. >> v*b ??? Error using ==> * Inner matrix dimensions must agree. >> v*b' ans = 2 4 6 4 8 12 6 12 18 >> v'*b ans = 28.
  3. >>

Is everything in MATLAB an array? ›

Every variable in MATLAB® is an array that can hold many numbers.

Why are arrays important in MATLAB? ›

Matrices and arrays are the fundamental representation of information and data in MATLAB®. You can create common arrays and grids, combine existing arrays, manipulate an array's shape and content, and use indexing to access array elements.

Are matrices just arrays? ›

In mathematics, a matrix ( pl. : matrices) is a rectangular array or table of numbers, symbols, or expressions, with elements or entries arranged in rows and columns, which is used to represent a mathematical object or property of such an object.

What are the basic operations of MATLAB? ›

Basic Arithmetic
  • Addition. + Add numbers, append strings. sum. Sum of array elements. ...
  • Subtraction. - Subtraction. diff. ...
  • Multiplication. .* Multiplication. * ...
  • Division. ./ Right array division. .\ ...
  • Powers. .^ Element-wise power. ^ ...
  • Transpose. .' Transpose vector or matrix. ' ...
  • Array Sign. uminus. Unary minus. uplus.

Is MATLAB better than Python? ›

MATLAB's integration with Simulink and specialized toolboxes makes it an ideal choice for certain engineering applications. On the other hand, Python's vast ecosystem and interoperability work well with a broader range of applications and more collaborative-based tasks and projects.

What is basic operation on array? ›

Basic operations

Traversal - This operation is used to print the elements of the array. Insertion - It is used to add an element at a particular index. Deletion - It is used to delete an element from a particular index. Search - It is used to search an element using the given index or by the value.

What is an array of vectors? ›

We can think of a vector as a list that has one dimension. It is a row of data. An array is a list that is arranged in multiple dimensions. A two-dimensional array is a vector of vectors that are all of the same length.

What is vectorized array? ›

"Vectorization" (simplified) is the process of rewriting a loop so that instead of processing a single element of an array N times, it processes (say) 4 elements of the array simultaneously N/4 times.

What is vector example in MATLAB? ›

In MATLAB a vector is a matrix with either one row or one column. In two dimensional system, a vector is usually represented by 1 × 2 matrix. For example, a vector, B in Figure 1 is 6i + 3j, where i and j are unit vectors in the positive direction for x and y axes, respectively in the Cartesian coordinate system.

How do you check if an array is a vector MATLAB? ›

TF = isvector( A ) returns logical 1 ( true ) if A is a vector. Otherwise, it returns logical 0 ( false ). A vector is a two-dimensional array that has a size of 1-by-N or N-by-1, where N is a nonnegative integer.

Top Articles
Section 4Rs Dodger Stadium
Knoxville Tennessee White Pages
Uhauldealer.com Login Page
Unitedhealthcare Hwp
From Algeria to Uzbekistan-These Are the Top Baby Names Around the World
Apex Rank Leaderboard
Crossed Eyes (Strabismus): Symptoms, Causes, and Diagnosis
Here's how eating according to your blood type could help you keep healthy
William Spencer Funeral Home Portland Indiana
Winterset Rants And Raves
Blog:Vyond-styled rants -- List of nicknames (blog edition) (TouhouWonder version)
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
Los Angeles Craigs List
Raleigh Craigs List
Patrick Bateman Notebook
Obsidian Guard's Cutlass
Equibase | International Results
Erica Banks Net Worth | Boyfriend
Silive Obituary
Water Trends Inferno Pool Cleaner
Mail.zsthost Change Password
Dulce
Reicks View Farms Grain Bids
When His Eyes Opened Chapter 3123
Narragansett Bay Cruising - A Complete Guide: Explore Newport, Providence & More
Kuttymovies. Com
3 Ways to Format a Computer - wikiHow
Tripcheck Oregon Map
Craigslist Middletown Ohio
Poe T4 Aisling
Purdue Timeforge
R/Orangetheory
Siskiyou Co Craigslist
Gwen Stacy Rule 4
Here’s how you can get a foot detox at home!
The Wichita Beacon from Wichita, Kansas
Desirulez.tv
What Time Is First Light Tomorrow Morning
Kvoa Tv Schedule
Best Workers Compensation Lawyer Hill & Moin
R&J Travel And Tours Calendar
Tugboat Information
„Wir sind gut positioniert“
Other Places to Get Your Steps - Walk Cabarrus
Yale College Confidential 2027
Best Restaurant In Glendale Az
1Tamilmv.kids
Greg Steube Height
Cvs Minute Clinic Women's Services
Yoshidakins
Latest Posts
Article information

Author: Fr. Dewey Fisher

Last Updated:

Views: 5269

Rating: 4.1 / 5 (62 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Fr. Dewey Fisher

Birthday: 1993-03-26

Address: 917 Hyun Views, Rogahnmouth, KY 91013-8827

Phone: +5938540192553

Job: Administration Developer

Hobby: Embroidery, Horseback riding, Juggling, Urban exploration, Skiing, Cycling, Handball

Introduction: My name is Fr. Dewey Fisher, I am a powerful, open, faithful, combative, spotless, faithful, fair person who loves writing and wants to share my knowledge and understanding with you.