10 Vectors

Smart data structures and dumb code works a lot better than the other way around.
Eric S. Raymond – Open Source Software Developer
\text{Up to now, if we wanted} to use a value in a program we had to create a variable. It is a straightforward one to one relationship, one number – one variable, But what if we do not know when we write the program how many values are going to be needed? We could use an accumulator and a single variable multiple times, but then we lose knowledge of the individual values. And what if we need them?
You want to calculate the mean of a set of observations. Easy enough, if you have three observations then
mean = (x1 + x2 + x3) ./ 3;
Since you have three values you create three variables,  x1, x2, and x3. But if you need four you will have to change the code.
An alternative is to use a while loop
sum = 0.0;
count = 0;
while(count < n)
sum = sum + x;
count = count + 1;
end
mean = sum ./ n;
The while loop approach eliminates the need to know the number of observations, but you lose the individual values. This becomes an issue if you want to calculate the standard deviation.
What is needed is a method of data management that
  • can store multiple values
  • does not limit the number of values (can be one or one million)
  • allows access to the individual values
  • can be handled in the program like a single variable
The solution is to use a type of data structure called an array or a vector.

Manipulating Data in Vectors

Vector
A vector is a data structure that contains a group of elements.

The most common and widely used data structure is the one-dimensional array called a vector. It is similar to a variable but with an important difference – it can hold multiple pieces of data.

A vector consists of a set of elements. An element is the individual the data item that it being stored in the vector. Each element has an index starting at 1.

Warning
A vector can be used to store numerical values or characters, but not both.

 

If a program has a vector called vec, then vec(k) is the k^{\mbox{th}} element in the vector. k is the index of that element.
There are two types of vectors; row vectors and column vectors. Thinking of this in terms of matrix dimensions a row vector will have one row and n columns or a 1 \times n matrix of elements. A column vector will have m rows and only one column. This would make it an m \times 1 matrix of elements.

Creating a new vector

Vectors are only as useful as their data. But before they are used, they must be created. If a vector were a variable then the variable would be declared. But vectors are not variables in the traditional sense; they are an object. As we create an instance of an object. This process is often instantiating and object, or in this case instantiating a vector.
Instance
In object oriented programming an instance is an occurrence of the object. Creating this instance is called instantiating.

 

Vectors can be created in three ways; as an empty vector, by enumerating the individual elements, or by creating a range of data.

Creating an empty vector

The more common means of instantiating a vector consist of creating it by filling it with data. But there are many times when you may want a vector that that begins its lifetime with no elements. This is an empty vector.
  1. %  An empty vector is created by assigning the [] to it
  2. vector = [];

Syntax For Creating an Empty Vector

Once instantiated, an empty vector can be used in the same way as a vector that contains data.

Entering data by enumerating the elements

Enumeration:
Elements are entered into a vector as a finite comma or semicolon delimited list.

If the vector has only a few elements it can be created by enumerating; listing the individual elements in the vector separated by commas or spaces.

  1. %  Each element is entered in a comma delimited list
  2. rowVector = [e_1, e_2, e_3, ... , e_n];

Syntax For Enumeration of a Row Vector

To create a column vector the commas are replaced with semicolons.
  1. %  Each element is entered in a semicolon delimited list
  2. columnVector = [e_1; e_2; e_3; ... ; e_m];

Syntax For Enumeration of a Column Vector

Note:
Row vectors and column vectors behave similarly. The distinction is minor now but will become important when they are used with matrices. Unless you specifically need one or the other it will not normally matter which you use, but you should be consistent. If you use row vectors then stay with row vectors. If you use column vectors then stay with column vectors. Avoid mixing the two in a single operation.

Example Row and Column Vectors with Eight Elements demonstrates creating a row vector called dataRow with eight numerical elements, and another called dataColumn with five, on the command line.

  1. >> %  Row vector - each element is comma delimited list
  2. >> dataRow = [7, 5, 2, 9, 6, 8, 0, 4]
  3. dataRow =
  4.      7   5   2   9   6   8   0   4
  5. >> %  Column vector - each element is semicolin delimited list
  6. >> dataColumn = [3; 8; 1; 0; 5]
  7. dataColumn =
  8.      3
  9.      8
  10.      1
  11.      0
  12.      5
  13. >>

Row and Column Vectors with Eight Elements

Enumeration is the preferred choice if you just need to enter a small set of values that are not in particular order. But what if you need to create a vector with hundreds or thousands of values?

Entering the elements in a vector as a range

If the data is an increasing or decreasing series with a constant distance between points then you can create the vector as a range of values.
  1. %  Elements are determined by a range of values
  2. rangeVector = [start:step:stop];

Syntax For Indicating the Range of Elements in a Row Vector

In the range approach, you enter three colon delimited values; the starting point, the step size – the distance between each point, and the stopping value. While the first element of the vector will the starting value, the stopping value is a do not exceed value. This means that the final element will be at or below the stopping value but never above it.
Note:
The final value in a vector will not pass the stop value.

The step value is optional. If it is omitted then it defaults to one. A common use of this – as it was in the for loop – is to create a vector with a set of values 1, 2, 3,\cdots, n where n is set in the program.

  1. >> %  Row vector - unit step from one to eight
  2. >> dataRow = [1:8]
  3. dataRow =
  4.      1   2   3   4   5   6   7   8
  5. >> dataRow = [3.2:6.7]
  6. dataRow =
  7.      3.2   4.2   5.2   6.2
  8. >>

Vector with Range from 1 to 8

Of course the range can start or stop at any values, positive or negative. If the \mbox{\textbf{start}} < \mbox{\textbf{stop}} then the elements will be increasing. But if \mbox{\textbf{start}} > \mbox{\textbf{stop}}, and step is omitted an empty vector will be created.
  1. >> %  Row vector - increasing range
  2. >> incVector = [-4:5]
  3. incVector =
  4.      -4   -3   -2   -1   0   1   2   3  4  5
  5. >> %  Row vector - decreasing range
  6. >> decVector = [6:-2]
  7. decVector = [](1x0)
  8. >>

Increasing and Decreasing Range of Elements in a Vector

Warning:
If start > stop and step is omitted an empty vector will be created. No warning or error will be given.

 

The empty vector, as in example Increasing and Decreasing Range of Elements in a
Vector
is created and is accessible. It is a vector like any other it just does not contain any elements. The notation (1 \times 0) indicates that it is a row vector; one row with zero columns.
If \mbox{\textbf{start}}\le\mbox{\textbf{stop}}<\mbox{\textbf{start} + 1} then the vector will contain only a single element. This also covers the case where  \mbox{\textbf{start}}=\mbox{\textbf{stop}} (example : Creation of a Scalar When stopstop + 1 ).
  1. >> %  stop = start
  2. >> scalar = [2:2]
  3. scalar =
  4.      2
  5. >> %  stop < start + 1
  6. >> scalar = [5:5.7]
  7. scalar =
  8.      5
  9. >>
Creation of a Scalar When \mbox{\textbf{stop}}\le\mbox{\textbf{stop}}+1
When the step is set the vector is created where each element is step away from the previous element. The final element will not always be stop. As mentioned previously, stop is a do not exceed value. This means that if adding an additional step to value results in an element that is beyond stop then the vector stops with lower value.
  1. >> %  Row vector - increasing range
  2. >> stepVector = [2:0.7:6]
  3. stepVector =
  4.      2.7   3.4   4.1  4.8   5.5
  5. >> %  Row vector - decreasing range
  6. >> decVector = [6:-1.3:-2]
  7. decVector =
  8.      6  4.7  3.4  2.1  0.8  -.5  -1.8
  9. >>

Elements in a Vector Where the Stopping Value is not an Element

The elements are created using the recursive formula

(1)   \begin{eqnarray*} a_k &=& a_{k-1} + \mbox{\textbf{step}}\nonumber\\ a_1 &=& \mbox{\textbf{start}}\nonumber\\ k &=& 2, 3, \ldots, [[1 + (\mbox{\textbf{stop}} - \mbox{\textbf{start}}) / \mbox{\textbf{step}}]] \end{eqnarray*}

Greatest Integer Function:
The function [[x]] is the greatest integer function (GIF). It is the largest integer that is less than or equal to x. In MatLab the GIF can be called using floor(x).

The recursive formula shows that elements will be created in both increasing series, \mbox{\textbf{step}} > 0, and decreasing series, \mbox{\textbf{step}} < 0. But if step is in contradiction with the direction of start and stop then the recursive series fails. In this case you will still create a vector, but it will be empty.

Warning:
While vectors can be created that are both increasing and decreasing, the sign of step must correspond to the whether the vector elements are increasing or decreasing. If they do not, the vector will be created but it will be empty.

The range method can only be used to create row vectors. If you need to create a column vector then you do so in two steps. First you create the row vector then you take the matrix transpose using the transpose operator (.’). This will transform the row vector into a column vector.

  1. >> %  Create the range then take transpose
  2. >> colVector = [0:4].'
  3. colVector =
  4.      0
  5.      1
  6.      2
  7.      3
  8.      4
  9. >>

Creating a Column Vector Using the Range of Elements

Assigning elements individually

Creating a vector by either enumerating all of the elements or by setting up a range of values is a common method if the values of the elements are know prior to running the program. But there are often times when you need to enter the value of an element one element at a time during the runtime. You can do this by assigning a value to an individual element.
  1. %  Syntax For Entering Individual Elements
  2. vector(k) = [e];
Syntax for Creating a Vector by Assigning Individual Elements
An example of this is shown in figure Entering Individual Elements where a new vector is created by first assigning a value to the first element; then the second, and the third, and so on.
  1. >> %  Enter values into the individual elements by their index
  2. >> vector(1) = 3;
  3. >> vector(2) = 5;
  4. >> vector(3) = 2;
  5. >> vector(4) = 7
  6. vector =
  7.      3  5  2  7
  8. >>
Entering Individual Elements
This is a common approach for when the user will be entering the data through an input prompt as demonstrated in example : Filling a Vector Using a while Loop and a Sentinel Value.
  1. function driver( )
  2. % DRIVER driver( ) is the main or driver function
  3.   % Create an empty vector
  4.   X = [ ];
  5.   % Create a counter and an input variable
  6.   count = 0;
  7.   x = 0;
  8.   %  Enter data using a while loop
  9.   %  Use a sentinel value (< 0) to stop
  10.   fprintf('Enter each value\n');
  11.   fprint('(negative to quit)\n');
  12.   while(k > 0)
  13.      k = input('Next value: ');
  14.      if(x > 0)
  15.         count = count + 1;
  16.         X(count) = x;
  17.      end
  18.   end
  19.   %  Print the vector using a for loop
  20.   fprintf('\n X = ');
  21.   for k = [1:count]
  22.      fprintf('%d: x = %0.2f    ', k, X(k));
  23.   end
  24.   %  Print a new line
  25.   fprintf('\n ', k, X(k));
  26. end
  1. >> driver( )
  2. Enter each value
  3. (negative to quit)
  4. Next value: 7
  5. Next value: 3
  6. Next value: 6
  7. Next value: -5
  8. X = 7.00     3.00    6.00
  9. >>
Filling a Vector Using a while Loop and a Sentinel Value
The example also shows a means of printing the elements of a vector. Since each element can be accessed by its index, a for loop that iterates through a list of the vector indices will also access each element.
Note:
If the index exceeds the current size of the vector, the vector will be expanded and the missing elements will be assigned the value 0.

 

There is no requirement that the elements be entered in order. If the index of an element that is being entered is beyond the final element of the current vector, or not 1 for a new vector, the vector that you create will be filled with zeros up to the index specified. This will also create a new vector if one does not already exist.

  1. >> %  Enter individual element
  2. >> vector(4) = 3;
  3. vector =
  4.      0  0  0  3
  5. >> vector(7) = 8;
  6. vector =
  7.      0  0  0  3  0  0  8
  8. >>
Buffering Vector Elements with Zeros
This technique can be useful when creating a vector of all zeros – although we will later see another way. Assign a value of 0 to what is to be the last element of a vector. It will be set to 0 as will all of the elements that come before it.
  1. >> %  Enter individual element
  2. >> zeroVec(7) = 0;
  3. zeroVec =
  4.      0  0  0  0  0  0  0
  5. >>
Creating a Zero Vector

Reassignment of Elements in a Vector

Vectors are mutable – a term that simply means that the individual elements can be reassigned. Changing individual elements is a matter of reassigning a value to a particular element. This can be done individually – one element at time – or by range.
Mutable:
An object if mutable if their components can be changed after they have been assigned.

Assigning a scalar to a single element

The syntax for changing a single element is shown in  figure Syntax for Changing an Individual Element of a Vector .
  1. %  Assigning a scalar to an element by indicating the index, k
  2. vector(k) = [x];

Syntax for Changing an Individual Element of a Vector

When assigning data to a single element of a vector the index of the element is within a set of parentheses, ( ), while the new element can – but is required to –  be within a set of square brackets [ ]. This creates a clear demarcation between the data in the element and location or index of that element. The sample code in figure  Changing an individual element of a vector demonstrates this.
  1. >> X = [6 3 8 1 4];
  2. >> X(2) = [5]
  3. X =
  4.    6 5 8 1 4
  5. >>
Changing an individual element of a vector
Warning:
When reassigning an individual element or a range of elements, the index values are indicated with the vector inside of the parentheses. The values of the elements that will be stored in the vector are collected inside of square brackets.

Assigning a vector to a range of elements

It is also possible to reassign a range of elements using a single command. It is very similar to an assignment to a single element, but the index is replaced with a range of indices, and the scalar value for the element is replaced with a vector. The syntax for this is shown in example Syntax for Changing a Range of Elements of a Vector .
  1. %  Assigning a vector to a range indicated by start:step:stop
  2. vector(start:step:stop) = [e_1 e_2 ... e_n];
Syntax for Changing a Range of Elements of a Vector
There are several items that need clarification in how updating a range of elements is done.
  • Because the range corresponds to the index values in the vector, the values of the range must be integers.
  • The minimum value for the range is 1.
  • If the step is excluded then it defaults to 1.
  • The range can be increasing or decreasing, but if it is decreasing then the step must be included.
  • If the length of the range of elements to be assigned in the range on the left of the assignment operator is n, then the length of the vector with the data to be assigned must be either n or one.
In this sample code, a range of three is indicated by the (2:4) and the vector on the right hand side has three elements. Thus the element values 3, 8, 1 are reassigned to 5, 9, 0.
  1. >> X = [6 3 8 1 4];
  2. >> X(2:4) = [5 9 0]
  3. X =
  4.    6 5 9 0 4
  5. >>
Assigning a vector of elements to a subrange of elements of another vector
Scalar Expansion:
If a vector is being assigned a scalar to a range of elements, the scalar is automatically repeated using scalar expansion until it is the same length as the vector.

What if the right hand side vector has a length of one – that is it only contains a single element? Then the single element is expanded to the same length of the vector using what is known as scalar expansion. This means that the scalar is repeated the number of times that are needed to make it appear as a vector with all elements the same.

  1. >> X = [6 5 9 2 4 1 8 7 3];
  2. >> X(3:5) = [3]
  3. X =
  4.    6 5 3 3 3 1 8 7 3
  5. >>

Assigning a scalar to a range of elements in a vector using scalar expansion

The step parameter makes it possible to assign data to noncontiguous elements of the vector. For example, if you need to change every third element to 0 then you set step to 3 as in figure Using step to reassign data to noncontiguous elements .
  1. >> X = [6 5 9 2 4 1 8 7 3];
  2. >> X(1:3:9) = [0]
  3. X =
  4.    0 5 9 0 4 1 0 7 3
  5. >>

Using step to reassign data to noncontiguous elements

This will also work right to left – a decreasing range – as long as \mbox{\textbf{start}} > \mbox{\textbf{stop}} and \mbox{\textbf{step}} < 0. As always start, step, and stop must be integers.
  1. >> X = [6 5 9 2 4 1 8 7 3];
  2. >> X(9:-4:1) = [0]
  3. X =
  4.    0 5 9 2 0 1 8 7 0
  5. >>

Using a negative step to reassign from right to left

In addition to mutability – the ability to reassign values to the elements of a vector – the length of the vector in terms of the number of elements will increase and decrease as elements are added or removed. This means that vectors are dynamic.

Dynamic Vectors

We have seen that we can create vectors and add elements to that vector by assigning a value to an individual element. If the vector does not exist then it is created with the first element assignment. If it does, and the index is beyond the last element of the vector, then the vector is expanded to include the new element. As we have seen the expansion may include the creation of additional elements of which each is assigned the value 0. This demonstrates that vectors are dynamic – they can grow with the addition of data. We also see that they can shrink as elements are removed from the vector.
Dynamic Vectors:
A vector that can have data added to it and thus increase the number of elements, or data deleted decreasing the number of elements is a dynamic vector.

Vectors, being dynamic, can increase in length by adding elements or decreased by deleting elements.

Appending elements onto a vector

We have already seen an example of expanding a vector; assigning a value to an element beyond the current end of the vector. Examples of this are in figures Syntax for Creating a Vector by Assigning Individual Elements  and  Buffering Vector Elements with Zeros. In both cases this is known as appending – adding data to then end of a list or a vector.

Appending

Appending data consists of adding elements to the end of a vector, thus increasing the length of the vector.

  1. %  Appending elements after the end of a vector of length n
  2. %  Note m > n and is an integer
  3. vector(m) = [e_1 e_2 ... e_k];

Syntax for Appending Elements onto a Vector

Appending data is not limited to single elements. Vectors can be appended onto the tail of a vector as long as the range of the index values matches the number of elements in the vector to be appended. As with scalars, if you append a vector at a point beyond the current final element then the additional elements will be created with the value 0. The step parameter can also be set so that the vector being appended is not contiguous but instead separated by elements with the value 0.
  1. >> X = [3 7];
  2. >> % Append a vector onto the tail
  3. >> X(3:4) = [5 1]
  4. X =
  5.    3 7 5 1
  6. >> % Append a vector beyond the tail with a step value
  7. >> X(7:2:9) = [6 8]
  8. X =
  9.    3 7 5 1 0 0 6 0 8
  10. >> % Append a scalar with a repeat of the value
  11. >> X(10:2:12) = [2]
  12. X =
  13.    3 7 5 1 0 0 6 0 8 2 0 2
  14. >>

Appending a vector onto the end of another vector

Inserting elements within a vector

It is also possible to add elements to the head of a vector – the beginning – or the middle. Unlike appending this is known as insertion.
There is a slight difference in the syntax when inserting a vector at the head or tail than there is when inserting it within the middle of a different vector.
  1. %  Inserting elements at the head of a vector
  2. vector = [ [e_1 e_2 ... e_k] vector];
  3. %  Inserting elements at the tail of a vector
  4. vector = [ vector [e_1 e_2 ... e_k] ];

Syntax for Inserting Elements at the Head or Tail of a Vector

Insertion consists of creating a new vector consisting of the current vector and the scalar or vector to be inserted. Once created this new vector is assigned to the current vector, replacing it.

The difference that occurs when inserting within the vector is that the original vector must be split at the insertion point. The portion that is to remain in the front of the insertion point is indicated by its range while the remaining subvector is placed after the inserted vector and is indicated by its original range.

Warning:
When inserting a vector into the middle, the ranges that are indicated are those of the current vector before the insertion.
  1. %  Inserting elements after the mth element of a vector of length n
  2. %  Note 1 <= m <= n and is an integer
  3. vector = [vector(1:m) [e_1 e_2 ... e_k] vector(m+1:n)];

Syntax for Inserting Elements into a Vector

In figure Inserting a vector onto the head and in the middle of another vector  the second insertion takes place at index 4. Thus the first three elements of the current vector are placed in the new, temporary, vector. After the vector that is to be inserted, the remaining elements of the original vector are placed at the tail. Since the remaining vectors started at the old index of 4 the are indicated with the old index value despite now being at index 6.
  1. >> X = [3 7];
  2. >> % Insert a vector at the head
  3. >> X = [[5 1] X]
  4. X =
  5.    5 1 3 7
  6. >> % Insert a vector in the middle
  7. >> X = [X(1:3) [6 8] X(4)]
  8. X =
  9.    5 1 3 6 8 7
  10. >>
  11. >> % Insert a vector at the tail
  12. >> X = [X [4 2]]
  13. X =
  14.    5 1 3 6 8 7 4 2
  15. >>

Inserting a vector onto the head and in the middle of another vector

Inserting elements can be implemented individually – as a single element – or as an additional vector. With the insertion approach a new – albeit unnamed vector is temporarily created. Once the insertion is completed the temporary vector is assigned to the original vector – replacing the old with the new.
Note:
Appending elements onto the tail of a vector allows the ability to buffer missing elements with zeros and also the ability to set a step value to have the appended vector skip elements by adding in zero values. Inserting does not. Any vector or scalar inserted will done by creating a gap in the original vector and filling that gap with the new elements.

 

The number of elements in a vector can be increased by insertion anywhere in the vector – head, middle, or tail – and also by appending onto the tail.
But in keeping with the dynamic nature of vectors – if elements can be added they should be able to be removed. Thus elements can be deleted as well.

Deleting elements from a vector

Deleting an element or a set of elements from a vector is accomplished in the same way if the elements are at the head, the middle, or the tail. In fact it is not so much a matter of deleting the elements as it is reassigning them with … nothing.
To delete an element, a range of elements, or selected elements you reassign them with the empty vector, [ ].
  1. %  Deleting elements from an interval of a vector
  2. vector(start:step:stop) = [];
Syntax for Deleting Elements from a Vector
In its most basic – deleting a single element – you set that element to the empty vector. After doing so will realign the indices for the vector so that all of those after the deleted element will shift one to the left.
  1. >> X = [1:15];
  2. >> % Delete the fifth element
  3. >> X(5) = []
  4. X =
  5.    1 2 3 4 6 7 8 9 10 11 12 13 14 15
  6. >> % Delete the current 7th through 9th element
  7. >> X(7:9) = []
  8. X =
  9.    1 2 3 4 6 7 11 12 13 14 15
  10. >>
  11. >> % Delete every third element of those remaining
  12. >> X(3:3:length(X)) = []
  13. X =
  14.    1 2 4 6 11 13 14
  15. >>

Deleting an Element or a Range of Elements from a vector

Appending and inserting elements is reversible. As long as you retain the length of the original vector or the location of the inserted elements you can then delete them. But not so with deletion. Once elements are deleted they are gone.

Operations With Vectors

Vectors provide a means of storing and moving data through a program with only a single variable. This includes passing vectors to a function and returning them from functions. Whether the operations with vector take place within a function or outside of one, the computations can take advantage of their multiple data elements.
These calculations happen in two ways; as one-dimensional matrices or as individual elements. The first will follow the properties of matrix operations while the latter is done element-wise.

 Element-Wise Operations

 The most common use of vectors is in element-wise operations. In this type of operation computations are performed between a vector and a scalar, or two vectors of the same type and size.
Element-wise operations with vectors are operations in which the computation is performed between the elements in the first vector with the elements in the same location of the second. Thus if you have two vectors, X and Y then the first element of X will be matched with the first element of Y. The second of each will be paired, and the third, and so forth.
 If we use \odot to indicate any element-wise operation then

(2)   \begin{eqnarray*} Z &=& \left[a_1, a_2, \ldots, a_n\right]\odot\left[b_1, b_2, \ldots, b_n\right]\nonumber\\ &=& \left[ a_1\odot b_1, a_2\odot b_2,\ldots, a_n\odot b_n\right] \end{eqnarray*}

Warning:
The sample code in figure Adding the elements of two vectors using a for loop is included to show how an element-wise operation could be written. It is included simply as a chance to look under the hood. You should not actually write your vector operations using for loops but instead use the element-wise operators.

While element-wise operations are built in as callable operators, it is possible to write code using a single for loop that perform an element-wise operation on two vectors – or a vector and a scalar. A demonstration of this in figure Adding the elements of two vectors using a for loop.

  1. function driver( )
  2. % DRIVER driver( ) is the main or driver function
  3.   %  Set the length
  4.   n = 8;
  5.   %  Create two vectors
  6.   X = [3, 4, -3, 6, 8, -7, -1, 9];
  7.   Y = [6, 1, 5, 9, 7, 2, -3, 4];
  8.   % Start a for loop to add the elements of the
  9.   % two vectors. The iterator a list from 1 to n
  10.   for k = 1:n
  11.      %  Add the corresponding elements
  12.      Z(k) = X(k) + Y(k);
  13.   end
  14.   %  Show the elements of Z
  15.   Z
  16. end
  1. >> driver( ) 
  2. Z =
  3.   9  5  2  15  15  -5  -4  13
  4. >>
Adding the elements of two vectors using a for loop
 Although any of the element-wise operations can be written using a for, loops, especially iterative loops, are considered slow when using an interpreted language. As such they should be avoided. Instead MatLab provides built-in operators for the five element-wise operations between vectors, addition +, subtraction -, multiplication .*, division ./, and exponentiation .\pow.
  1. %  Addition
  2. vectorSum = vec_1 + vec_2;
  3. %  Subtraction
  4. vectorDifference = vec_1 - vec_2;
  5. %  Product
  6. vectorProduct = vec_1 .* vec_2;
  7. %  Quotient
  8. vectorQuotient = vec_1 ./ vec_2;
  9. %  Power
  10. vectorPower = vec_1 .^ vec_2;
Syntax for the Five Element-Wise Operations
Element-wise calculations are the standard when performing any calculation with vectors as examples Demonstrating Element-Wise Operations with Two Vectors  and Demonstrating Element-Wise Operations with Vector and Scalar  show.
Note:
A question about calculations is often asked. “Why use .*, ./, and .\pow instead of the the commonly accepted .*, ./, and .\pow? The more common operators are used for matrix operations. The dot operator is used to differentiated matrix from element-wise operations.

 

A question is often asked about calculations. “Why use .*, ./, and .\pow  instead of the same operator without the dot? After all, it works just fine with scalars.”
The answer is that all operations were originally meant to be used with matrices in matrix operations. Because of this the common operators for multiplication, division, and exponentiation were assigned to their matrix calculation. And yes, it does work for scalars because a scalar is a 1 \times 1 matrix and for scalars the matrix operations and the element-wise operations are the same. But they are not the same for vectors or other matrices.
This is also the reason that you do not need the dot for + or . Matrix addition and subtraction are the same as element-wise addition and subtraction. Since there is no difference there is no need for the dot.
  1. function driver( )
  2. % DRIVER driver( ) is the main or driver function
  3.      % Create vectors
  4.      X = [6, 3, 9, 7]
  5.      Y = [2, 0, 3, 1]
  6.      %  Addition
  7.      A = X + Y
  8.      %  Subtraction
  9.      S = X - Y
  10.      %  Multiplication
  11.      M = X .* Y
  12.      %  Division
  13.      D = X ./ Y
  14.      %  Exponentiation
  15.      E = X .^ Y
  16. end
  1. >> driver( )
  2. X =
  3.    6   3   9   7
  4. Y =
  5.    2   0   3   1
  6. A =
  7.    8   3   12   8
  8. S =
  9.    4   3   6   6
  10. M =
  11.    12   0   27   7
  12. D =
  13.    3   inf   3   7
  14. E =
  15.    36   1   729   7
  16. >>
Demonstrating Element-Wise Operations with Two Vectors
If the operations are between a vector and a scalar then the scalar expansion is performed so that the scalar is the same length as the vector. From there the usual element-wise operations are done. Of course the scalar expansion is never seen.
  1. function driver( )
  2. % DRIVER driver( ) is the main or driver function
  3.      % Create a scalar and a vector
  4.      a = 4;
  5.      X = [2, 0, 3, 1];
  6.      %  Addition
  7.      A = a + X
  8.      %  Subtraction
  9.      S = a - X
  10.      %  Multiplication
  11.      M = a .* X
  12.      %  Division - Scalar Denominator
  13.      DSD = a ./ X
  14.      %  Division - Vector Denominator
  15.      DVD = a ./ X
  16.      %  Exponentiation - Scalar Base
  17.      ESB = a .^ X
  18.      %  Exponentiation - Vector Base
  19.      EVB = X .^ a
  20. end
  1. >> driver( )
  2. A =
  3.    6   4   7   5
  4. S =
  5.    2  -4   1  3
  6. M =
  7.    8   0  12   4
  8. DSD =
  9.    0.5   0   0.75   0.25
  10. DVD =
  11.    2   inf   1.33   4
  12. ESB =
  13.    16   1   64   4
  14. EVB =
  15.    16   0   81   1
  16. >>
Demonstrating Element-Wise Operations with Vector and Scalar

Matrix Operations with Vectors

While we commonly think of vectors as a data structure to store and manipulate multiple elements, they are at the most basic a one-dimensional matrix. A row vector is a 1 \times n matrix and a column vector is an m \times 1 matrix.
Note:
Using the operator with the dot or without when adding, subtracting, or multiplying a vector and scalar will not change the result. A matter of consistency is to choose the operator based upon whether the operations are meant to be matrix or element-wise. If matrix operations then do not use the dot. If element-wise then use the dot.

 

Matrix operations with a vector and a scalar are the same regardless of whether it is done using element-wise or matrix calculation. As such, if you are adding, subtracting, multiplying a vector and a scalar the choice of operator, with the dot or without, does not matter.
If the row vector is multiplied to the column vector then each must have the same dimension (number of elements). But the dimensions of the two vectors do not have to be the same if it is the column vector multiplying the row vector. Recall that when multiplying matrices the columns of the first must match the rows of the second. Thus for two vectors

(3)   \begin{eqnarray*} (1 \times n) * (n \times 1) &= 1 \times 1 & (\mbox{scalar})\nonumber\\ (n \times 1) * (1 \times m) &= n \times m & (\mbox{matrix}) \end{eqnarray*}

The first product, the scalar, is the dot product or scalar product. It is a common calculation in engineering for calculating the magnitude of a vector or the angle between two vectors.
Dot Product:
The dot product, or scalar product is the matrix product of a row vector and a column vector of the same length. It is calculated as the sum of the products of each matching element. It can be used to determine the angle between two vectors.

 

The magnitude of a vector is its length. It is calculated by

(4)   \begin{eqnarray*} \left |u\right |  &=& \sqrt{u\cdot u^T} \end{eqnarray*}

The magnitude can be calculated as a single line code – that can be run on the command line – in which a vector is multiplied using the matrix product with its own transpose (figure Dot product to calculate the magnitude of a vector ).
  1. >> X = [4, -3, 6];
  2. >> % Calculate the magnitude as the matrix product with its transpose
  3. >> magX = sqrt(X * X.')
  4. magX =
  5.      7.8102
  6. >>

Dot product to calculate the magnitude of a vector

The dot product can be used to calculate the angle between two vectors.

(5)   \begin{eqnarray*} u \cdot v &=& \left |u\right | \left |v\right | \cos(\theta)\nonumber\\ \theta &=& \cos^{-1}\left(\frac{u \cdot v}{\left |u\right | \left |v\right | }\right) \end{eqnarray*}

 there are two different vectors then the dot product can be used to calculate the
The sample code in figure  Dot product to calculate the angle between two vectors demonstrates using the vector product to calculate the angle between two vectors in 3-space.
  1. function driver( )
  2. % DRIVER driver( ) is the main or driver function
  3.   % Create two vectors
  4.   X = [4, -3, 6];
  5.   Y = [1, 5, -2];
  6.   % Calculate the magnitude of each of the two vectors
  7.   % Need to take the transpose of the second vector
  8.   magX = sqrt(X * X.');
  9.   magY = sqrt(Y * Y.');
  10.   % Calculate the dot product by multiplying two vectors
  11.   % Need to take the transpose of the second vector
  12.   dotProduct = X * Y.';
  13.   % Calculate the angle between the vectors
  14.   angle = acosd(dotProduct ./ (magX .* maxY);
  15.   % Print the results
  16.   fprintf('Angle: %0.2f degrees\n', angle);
  17. end
  1. >> driver( )
  2. Angle: 122.52 degrees
  3. >>
Dot product to calculate the angle between two vectors
Vectors calculations can be made both element-wise and matrix wise. This puts the vector on the same level as scalars in terms of calculation, but can vectors be compared to one another? And what would comparing two vectors even mean?

Vectors, Relational Operators, and Comparison

Is it possible for a vector to be smaller, or larger than a scalar? How about two vectors? Can one vector be smaller than the second. In a basic sense – no. The magnitude of each vector can be compared or the magnitude of a vector can be compared to a scalar, but not the entire vector.
Comparing the magnitude of two vectors – or the magnitude of a vector to a scalar is an application of the dot product. As an example, figure Comparing the magnitude of two vectors  demonstrates how to determine if the magnitude of one vector is larger than than other.
  1. function driver( )
  2. % DRIVER driver( ) is the main or driver function
  3.   % Create two vectors
  4.   X = [4, -3, 6];
  5.   Y = [1, 5, -2];
  6.   % Calculate the magnitude of each of the two vectors
  7.   % Need to take the transpose of the second vector
  8.   magX = sqrt(X * X.');
  9.   magY = sqrt(Y * Y.');
  10.   % Compare the vector magnitudes
  11.   if(magX > magY)
  12.      fprintf('Vector X is larger than Vector Y\n');
  13.   else
  14.      fprintf('Vector X is not larger than Vector Y\n');
  15.   end
  16. end
  1. >> driver( )
  2. Vector X is larger than Vector Y
  3. >>
Comparing the magnitude of two vectors
Since in engineering we often use vectors in their traditional sense of a ray with direction and magnitude, this application would be used to determine which vector is longer. But this application is not a comparison of vectors as much as it is a comparison of scalars – which we have done before.
Comparing vectors as data structures is different from comparing them as geometrical vectors. In the data structure implementation vectors are compared element-wise. This means that if a relational operation between two vectors, X and Y will result in a third vector where each element indicates with true or false (1 or 0)
  1. function driver( )
  2. % DRIVER driver( ) is the main or driver function
  3.   % Create two vectors
  4.   X = [3, 4, -3, 6, 8, -7, -1, 9];
  5.   Y = [6, 1, 5, 9, 7, 2, -3, 4];
  6.   % Compare the vectors element-wise
  7.   % This will result in a vector of 0s an d1s
  8.   TorF = (X < Y)
  9.   % Number of ones
  10.   numOnes = TorF * TorF.'
  11.   % Number of zeros
  12.   numZeros = (1-TorF) * (1-TorF).'
  13. end
  1. >> driver( )
  2. TorF =
  3.   1  0  1  1  0  1  0  0
  4. numOnes =  4
  5. numZeros =  4
  6. >>

Comparing the elements of two vectors

In this example, figure Comparing the elements of two vectors, each element is compared with the result being its logical value.
Note
A useful addition to this would be know how many of the elements are zero or one. While we will later see several functions that will do this directly we could adapt the code to provide us the number of 1s and the number of 0s. Recall that the dot product of a vector with itself will return a sum of the squares of the elements. And since all of the elements are either 1 or 0 then the sum of squares will be the number of true values. If we then subtract each element from 1 and repeat it we will have the number of zeros.

This same approach is used when comparing a vector to a scalar. In this matter the scalar is expanded to the same length of the vector and each element of the vector is then compared to the same value. This can be used, as in the example in figure Comparing the elements of a vector with a scalar to see if all of the elements are above some minimum value. And if they are not, which elements fail the test.

  1. function driver( )
  2. % DRIVER driver( ) is the main or driver function
  3.   % Create two vectors
  4.   X = [5, -5, 2, 9, -1, 3, 0, 6];
  5.   a = 0;
  6.   % Compare the vector to the scalar
  7.   % This will result in a vector of 0s an d1s
  8.   TorF = (X >= a)
  9.   % Number of ones
  10.   numOnes = TorF * TorF'
  11.   % Number of zeros
  12.   numZeros = (1-TorF) * (1-TorF).'
  13. end
  1. >> driver( )
  2. TorF =
  3.   1  0  1  1  0  1  1  1
  4. numOnes =  6
  5. numZeros =  2
  6. >>

Comparing the elements of a vector with a scalar

Vector operations can be implemented both element-wise and matrix wise. They can also be compared element-wise to another vector or by scalar expansion to a single scalar. We have developed these operations to be performed outside of functions, but in keeping with our goal of top-down design we need to be to hide them away within functions as well.

Vectors and Functions

The relationship between vectors and functions are similar to those with scalars. Vectors can be passed as a parameter to a function, and they can be returned as an output from a function. But is also possible to have scalar inputs with vector outputs, vector inputs with vector outputs, and vector inputs with scalar outputs.

Functions that are passed vectors and return vectors

Most functions in which a vector is passed returns a vector consisting of the elements calculated using element-wise operations. This is the case for any of the computation functions like the trigonometry functions sin, cos and tan. This is the same for the algebraic functions such as log, exp.
An example of a code in which a vector is passed to the cos function and the return vector is then part of an additional element-wise calculation is shown in figure Element-Wise Operations in Evaluating a Built-In Function.
  1. >> % Create vector
  2. >> X = [0, 30, 45, 60, 90]
  3. >> %  Evaluate the cosine with angle in degrees
  4. >> Y = cosd(X)
  5. Y =
  6.    1.00000   0.86603   0.70711   0.50000   0.00000
  7. >>
Element-Wise Operations in Evaluating a Built-In Function
Element-wise calculations with a vector return are the common result of most local and anonymous functions.
  1. >> % Create the anonymous function
  2. >> f = @(x) x.^2 + 3.*x - 10;
  3. >> X = [-3, -2, -1, 0, 1, 2, 3, 4];
  4. >> % Evaluate the function
  5. >> Y = f(X)
  6. Y =
  7.      -10 -12  -12  -10   -6    0    8   18
  8. >>
Element-Wise Calculations in an Anonymous Function
It should not be assumed that functions that are passed and return vectors are simply computational. There are many applications in which you need to rearrange elements in a vector or sort them in ascending – lowest to highest – or descending – highest to lowest – order. These will be addressed in section Sorting Elements in a Vector.

Functions that are passed scalars and return vectors

Most of the functions with which we have dealt have been computational; pass scalar values as input parameters – calculate and returns another scalar. With vectors and element-wise operations nothing changes. We pass a vector or vectors to the function, and it uses them to calculate and return another function.
But there are functions that are passed individual scalars and these are used to create a vector.
Example: A common issue in creating a vector is determining the correct step size for the range. Say you want a vector with just five elements from 0 to 100. Many people would erroneously say the step should be set at 20 but this would create a vector with six elements. Close, but six is not five.
step should not have been set to 20 but instead to 25.
 
The step size for a vector given the start and stop values with a set number of elements could be calculated in a function and the function could then create and return the vector with the correct number of elements. The step size – or the number of elements – can be calculated using the formula

(6)   \begin{eqnarray*} \mbox{\textbf{step}} &=& \frac{\mbox{\textbf{stop}} - \mbox{\textbf{start}}}{n-1} \end{eqnarray*}

The number of elements is decreased by one, n-1, because the divisor is not the number of elements but instead the number of steps. For any vector there will always be one less step than there are elements.
If needed, equation~6 can be solved for n to determine the number of elements.

(7)   \begin{eqnarray*} n  &=&\left [\left[ \frac{\mbox{\textbf{stop}} - \mbox{\textbf{start}}}{\mbox{\textbf{step}}} + 1\right]\right] \end{eqnarray*}

The formula in equation~6 can also be used to create a local function that creates a vector.
  1. function V = makeLinearVector(start, stop, n)
  2. %  MAKELINEARVECTOR V = makeLinearVector(start, stop, n) creates
  3. %  a row vector with elements spaced linearly starting at start and stopping
  4. %  at stop with n elements
  5.    %  Calculate the step size
  6.    step = (stop - start) ./ (n - 1);
  7.    %  Create the vector
  8.    V = [start:step:stop];
  9. end
  1. >> X = makeLinearVector(3, 17, 8)
  2. X =
  3.     3    5    7    9   11   13   15   17
  4. >>

Creating a Linearly Spaced Vector

linspace(start, stop, n):
The sample code in figure Creating a Linearly Spaced Vector demonstrates how to create a vector from the overall range and the number of elements. This code does not include the necessary error checking – \mbox{\textbf{stop}} \ge \mbox{\textbf{start}} and n > 0 and and also an integer, n\in\mathbb{Z}. While we could add this, there is already a built in function to do that for us, linspace(start, stop, n)

 

There is a built in function that does the same as the example function V = makeLinearVector(start, stop, n). It is V = linspace(start, stop, n).
In engineering it often occurs that we need a vector of values that spaced logarithmically, not linearly. For example, instead of a vector \mbox{\textbf{V}} = [0, 1, 2, \ldots, n], the vector needed is \mbox{\textbf{V}} = [10^0, 10^1, \ldots, 10^n]. This can be done using the makeLinearVector function (or linspace) and the element-wise power operator in figure Creating a Logarithmically Spaced Vector.
logspace(start, stop, n):
Much like linspace there is a built-in function to create the logarithmically space function. It is logspace(start, stop, n).
  1. function v_log = make_log_vector(start, stop, n)
  2. %  MAKE_LOG_VECTOR V = make_log_vector(start, stop, n) creates
  3. %  a row vector starting at 10.^start and stopping at 10.^stop with n elements
  4. %. logarithmically spaced
  5.    % Create a linear vector
  6.    v_lin = makeLinearVector(start, stop, n);
  7.    %  Element-Wise Power Operator
  8.    v_log = 10.^v_lin;
  9. end
  1. >> X = make_log_vector(0, 3, 8)
  2. X =
  3.       1.0000      5.6234     31.6228    177.8279   1000.0000
  4. >>
Creating a Logarithmically Spaced Vector
Note:
The built-in functions linspace and logspace perform the same actions, so why create local function? In this case it is a matter of generalization. The built-in function is convenient but the local function can be adapted to, for example, a binary growth function by changing V = 10.\pow VLin to V = 2.\pow VLin.
The sample function makeLogVector performs the same operation as calling the built-in function VLog = logspace(start, stop, n);
There are several functions similar to linspace and logspace but they are designed to return matrices. But they are useful enough in creating vectors to adapt them here. Three in particular are zeros, ones, and rand. So while we can use them as matrix functions and then force them to create vectors, we could just write our own local functions that create the vector directly.
The first two are easy. We can set start = 0, and stop = n and then fill the elements with the value 0 or 1.
  1. function zeroVector = makeZeroVector(n)
  2. %  MAKEZEROVECTOR zeroVector = makeZeroVector(n) creates a row
  3. %  vector starting of n elements all with the value 0
  4.    % Set start to 1 and use n for stop and use scalar expansion to
  5.    % fill all of the elements with zero
  6.    zeroVector[1:n] = 0;
  7. end
  1. >> Z = makeZeroVector(7)
  2. Z =
  3.       0     0     0     0     0     0     0
  4. >>
Creating a Zero Vector
While 0 and 1 are commonly used when creating an accumulator for sums or products, this technique can be used to create a vector with any starting value. For a different starting value, you change the assignment value in the function.
This type of function can actually be generalized for such as case by passing two input parameters to the function. The first is for the number of elements while the second is the value. If the user wants a default value, such as 0 then the function can use the nargin variable to overload it. An example of this is shown in figure Creating a Vector with a single repeated value.
  1. function valueVector = makeValueVector(n, a)
  2. %  MAKEVALUEVECTOR valueVector = makeValueVector(n) creates a
  3. %  row vector with n elements all with the value a
  4.    % Check nargin. If nargin == 1 then default to 0
  5.    if((nargin < 1) | (nargin > 2))
  6.       %  Exit with run time error for improper number of parameters
  7.       error('Incorrect number of inputs to makeValueVector');
  8.    elseif(nargin == 1)
  9.       %  Set to the default of 0
  10.       a = 0;
  11.    end
  12.    % Set start to 1 and use n for stop and use scalar expansion to
  13.    % fill all of the elements with a
  14.    valueVector[1:n] = a;
  15. end
  1. >> V = makeValueVector(7, 3)
  2. V =
  3.       3     3     3     3     3    3     3
  4. >>

Creating a Vector with a single repeated value

Functions that are passed vectors and return scalars

There are a class of functions that when they are passed a vector of data they analyze the vector and return some value based upon that analysis.
Probably the most useful of this type of function is one that calculates the number of elements in the vector. A function that is passed a vector could then count the number of elements using a for loop that iterates through the vector each time updating an accumulator.
  1. function n = length(X)
  2. % LENGTH n = length(X) counts the number of
  3. % elements in a vector
  4.   %  Set the accumulator to 0
  5.   n = 0;
  6.   % Use a for loop to iterate through the vector
  7.   for k = X
  8.      %  Update the accumulator
  9.      n = n + 1;
  10.   end
  11. end
  1. >> n = length([6 2 3 8 1 0 9 3])
  2. n =
  3.   8
  4. >>

Function to return the number or elements in a vector

Warning:
MatLab provides a built-in function, length, that does the same operations as in the local function in figure Function to return the number or elements in a vector. As such there is little reason to add the local function to a program. Instead call n = length(X); directly when you need to know the number of elements.

It makes little sense to add this local function to a program since it has already been written and is built-in. The built-in function is length(X).

length:
The built-in function, length, returns the number of elements in a vector.

 

While you would probably not write this function, it demonstrates how a function can be passed a vector and then analyze the vector returning a result. There are many such direct applications that could be written but because of the utility have already been included as built-in functions.
An example of this has already been demonstrated with the calculation of the dot product. This can be rewritten as a function; either a local function or an anonymous function.
  1. function dp = dot_product(X, Y)
  2. %  DOT_PRODUCT dp = dot_product(X, Y) calculates the dot product of two
  3. %  vectors - it checks that the first vector is a row and the second a column vector
  4.    % Check nargin. If nargin == 1 then it calculates the magnitude
  5.    if((nargin < 1) || (nargin > 2))
  6.       %  Exit with run time error for improper number of parameters
  7.       error('Incorrect number of inputs to calculate dot product');
  8.    elseif(nargin == 1)
  9.       %  Set to the default of 0
  10.       dp = 0;
  11.    else
  12.    end
  13.    % Set start to 1 and use n for stop and use scalar expansion to
  14.    % fill all of the elements with a
  15.    valueVector[1:n] = a;
  16. end
  1. >> X = [4, 7, 3, 1];
  2. >> Y = [9, 2, 6, 5];
  3. >> D = dot_product(X, Y)
  4. D =
  5.       73
  6. >>

Dot Product Function

Sorting Elements in a Vector

It often occurs that the elements in a vector are not where we would like them to be. Perhaps two or more elements should be rearranged, or an entire vector needs to be sorted in either ascending or descending order. There is a built-in function to sort the elements of a vector in ascending order, but perhaps you need the vector in descending order. Or you only need several elements rearranged. In these cases you might want to customize the control of the elements.

Swapping elements in a vector

Sorting elements in a vector begins with being able to swap them. As an example, we want to swap the element at index k with the element at index j. This function, let’s call it swap, will require three inputs; a vector, and the two indices for the elements to be swapped.

Rendered by QuickLaTeX.com

  1. function V = swap(V, k, j)
  2. %  SWAP V = swap(V, k, m) swaps the
  3. %  elements in index k of the vector
  4. %  V with index j of the same vector
  5.    %  Copy the first element to temp
  6.    temp = V(k);
  7.    %  Reassign the first element with
  8.    %  the value in the second
  9.    V(k) = V(j);
  10.    %  Reassign the second element with temp
  11.    %  with the value in temp
  12.    V(j) = temp;
  13. end
  1. >> X = [1:5]
  2. X =
  3.     1    2    3    4   5
  4. >> X = swap(X, 2, 5)
  5. X =
  6.     1    5    3    4   2
  7. >>
Function to swap elements
Note:
A common error in a swap is omitting the intermediate temp variable. An analogy is to imagine a farmer with a pen of chickens and another with feed for the chickens to eat. He needs to swap the chickens and the feed. If he put the chickens directly in with the feed they would start to eat the feed. Similarly if he put the feed directly in with the chickens. Instead he needs a temporary third pen. He then transfers the chickens to the temporary pen. He then moves the feed to the now empty pen that previously was full of chickens. Once the feed has been moved he can then transfer the chickens over to their new pen – and then remove the temporary enclosure.
The swap function has multiple applications but probably the most common is in sorting the elements of a vector.

Sorting a vector

sort:
The sort function is passed a vector, either a row vector or a column vector, and it returns the vector sorted in ascending order. The syntax is
V_sorted = sort(V_unsorted);
If the input and output vectors are the same the original will be replaced. But if they are different then there will be two instances of the vector – one sorted and one not.

Vectors that are created using a range are already sorted; lowest to highest or highest to lowest. But if the vector was created by enumerating the elements or appending data, or if data is inserted into a vector it may no longer be sorted. There is a built-in function, sort, that will sort a vector in ascending order, but you may want to sort the data in descending order, or perhaps only sort every other element. As such there are many different types of sorts as well as techniques to sort the vectors.

A common, and easy to code – sort algorithm is called the bubble sort. This technique uses a pair of nested for loops. In this method the outer loop steps through the 1^{\mbox{st}} through n-1^{\mbox{st}} element of the vector. Each iteration starts the inner loop which compares the outer loop element to each element of the vector from the k+1^{\mbox{st}} to the n^{\mbox{th}} element. At each step if the elements are out of order then they are swapped – can be done with the swap function.

Rendered by QuickLaTeX.com

  1. function V = bubble_sort(V)
  2. %  BUBBLE_SORT V = bubble_sort(V) uses
  3. %  a bubble sort algorithm to sort
  4. %  the vector from lowest to highest
  5.    %  Outer loop
  6.    for k = 1:length(V) - 1
  7.       %  Start inner loop
  8.       for j = k+1:length(V)
  9.          %  Check order
  10.          if(V(k) > V(j))
  11.             %  Out of order so swap
  12.             V = swap(V, k, j);
  13.          end %  End of if
  14.       end  %  End of inner loop
  15.    end  %  End of outer loop
  16. end
  1. >> X = [4, 1, 5, 3, 2]
  2. X =
  3.     4    1    5    3   2
  4. >> X = bubbleSort(X)
  5. X =
  6.     1    2    3    4   5
  7. >>
Bubble sort function

License

Discover Computing Copyright © by Brian Adams. All Rights Reserved.