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.

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

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 a vector array in MATLAB? ›

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.

What is array operation 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.

How do you write 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].

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.

Can you multiply vectors in MATLAB? ›

Multiply Row and Column Vectors

Create a row vector a and a column vector b , then multiply them. The 1-by-3 row vector and 4-by-1 column vector combine to produce a 4-by-3 matrix. The result is a 4-by-3 matrix, where each (i,j) element in the matrix is equal to a(j).

What is array in MATLAB with example? ›

In MATLAB®, all variables are arrays, including scalars and structs. No matter what type of data you want, you will store it in an array. An array is a collection of elements with the same data type. A vector is a one-dimensional array, and a matrix is a two-dimensional array.

Why is vector better than array? ›

Memory utilization: Vector can save memory space if the number of elements increases after the array created by using dynamic memory allocation. Iterators: Vectors provide iterators that make it easy to traverse the elements, whereas arrays require manual pointer manipulation.

How to create an array in MATLAB? ›

To create an array with multiple elements in a single row, separate the elements with either a comma ',' or a space. This type of array is called a row vector. To create an array with multiple elements in a single column, separate the elements with semicolons ';'. This type of array is called a column vector.

Why is MATLAB better than Python? ›

Differences in community and support

MATLAB's extensive use in academia and industry is ideal for users who need specialized support in engineering and scientific disciplines. However, it is proprietary, which means all code is owned and licensed, and users can't build or add to its functionality. Python is not.

What is the difference between element-wise multiplication and matrix multiplication? ›

Elementwise multiplication (which uses the # operator) should not be confused with matrix multiplication (which uses the * operator). When an element of a matrix contains a missing value, the corresponding element of the product is also a missing value.

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

A vector is a 1-dimensional matrix, either a vertical vector (N × 1) or horizontal vector (1 × N). Vectors are a subclass of matrices, so every vector is a matrix. xL and xU are horizontal (1 × N) vectors and therefore they are also matrices. ib.

How to convert vector to binary in MATLAB? ›

To convert binary data from a string or character vector, you can use the MATLAB® function bin2dec .

Can you plot a vector in MATLAB? ›

Note that if you are working in 3D, you need to include the z components of the vectors and the starting points in the quiver function as well. These basic steps are to plot vectors in MATLAB using the quiver function. You can also use other functions, such as an arrow and compass, depending on your needs.

What are vectorized operations in MATLAB? ›

Vectorization is one of the core concepts of MATLAB. With one command it lets you process all elements of an array, avoiding loops and making your code more readable and efficient. For data stored in numerical arrays, most MATLAB functions are inherently vectorized.

Can we do vector integration in MATLAB? ›

Integrate Vector of Data with Unit Spacing

Create a numeric vector of data. Y = [1 4 9 16 25]; Y contains function values for f ( x ) = x 2 in the domain [1, 5]. Use trapz to integrate the data with unit spacing.

What are the logical operations on a vector in MATLAB? ›

If A is a vector, any(A) returns logical 1 (true) if any of the elements of A is a nonzero number or is logical 1 (true), and returns logical 0 (false) if all the elements are zero.

How do vector operations work? ›

Vector operations are fundamental mathematical operations that involve manipulating and combining vectors. They are governed by a set of simple laws. Vectors are quantities that have both magnitude and direction. Scalar quantities can be handled using simple algebraic rules.

Top Articles
Defiant (Skyward, #4)
Cytonic (Skyward, #3)
Morgandavis_24
Ascension St. Vincent's Lung Institute - Riverside
Academic Calendar Biola
Delta Rastrear Vuelo
Schluter & Balik Funeral Home Obituaries
Arre St Wv Srj
WWE Bash In Berlin 2024: CM Punk Winning And 5 Smart Booking Decisions
Two men arrested following racially motivated attack on Sanford teen's car
Chase Bank Pensacola Fl
Ter Reviews Boston
Cookie Clicker The Advanced Method
Chubbs Canton Il
Plan the Ultimate Trip to Lexington, Kentucky
Sound Of Freedom Showtimes Near Sperry's Moviehouse Holland
Cappacuolo Pronunciation
Regal Cinema Ticket Prices
Vegamovies 2023 » Career Flyes
Craigslist Columbus Ohio Craigslist
Craigslist For Cars Los Angeles
Edenmodelsva
C.J. Stroud und Bryce Young: Zwei völlig unterschiedliche Geschichten
1-800-308-1977
Stellaris Remove Planet Modifier
Shawn N. Mullarkey Facebook
Atdhe Net
10 018 Sqft To Acres
Full Volume Bato
Ring Of Endurance Osrs Ge
Coil Cleaning Lititz
Conan Exiles Meteor Shower Command
Barber Gym Quantico Hours
Terraria Water Gun
1084 Sadie Ridge Road, Clermont, FL 34715 - MLS# O6240905 - Coldwell Banker
Cnb Pittsburg Ks
Lincoln Access Rewards Redemption
Ixl Sbisd Login
Ucf Net Price Calculator
Allina Akn Network
Sveta Håkansson
Monte Carlo Poker Club Coin Pusher
Whitfield County Jail Inmates P2C
Concord Mills Mall Store Directory
Minute Clinic Schedule 360
2-bedroom house in Åkersberga
Dr Seuss Star Bellied Sneetches Pdf
Kgtv Tv Listings
C-Reactive Protein (CRP) Test Understand the Test & Your Results
German police arrest 25 suspects in plot to overthrow state – DW – 12/07/2022
Houses and Apartments For Rent in Maastricht
When His Eyes Opened Chapter 3002
Latest Posts
Article information

Author: Van Hayes

Last Updated:

Views: 5954

Rating: 4.6 / 5 (66 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Van Hayes

Birthday: 1994-06-07

Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

Phone: +512425013758

Job: National Farming Director

Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.