Pages - Menu

Showing posts with label vectors. Show all posts
Showing posts with label vectors. Show all posts

Wednesday, 10 October 2012

Defining vectors in MATLAB. Vector commands and built-in functions in MATLAB

This article will show you how to define and manipulate vectors in MATLAB using built-in functions.

A vector is a special case of a matrix. The elements of a vector must be enclosed in square brackets.

A row vector is a 1xn matrix. The entries of a row vector can be delimited by either spaces or commas. Below you have two vectors defined in both ways.
>> Rvec1=[1 2 3 4 5]
Rvec1 =
     1     2     3     4     5

>> Rvec2=[3.14,15,9,26]
Rvec2 =
     3.1400    15.0000    9.0000     26.0000

A column vector is a nx1 matrix. The components of a column vector are separated by a semi-colon.
>> Cvec1=[1;2;3;4;5]
Cvec1 =
     1
     2
     3
     4
     5

You can easily change the value of a element using MATLAB's indexing commands by typing the following in the command line:
>> Rvec1(1)=10
Rvec1 =
    10    2     3     4      5

As you can see, the first component of the vector has now the value 10.

Accessing multiple elements can be done as follows:
>> Rvec1([1 3 5])
ans =
    10    3    5

The snippet above lets you access the first, third and fifth element of the vector. You can change more than 1 element like this:
>> Rvec1([1 2])=[7 8]
Rvec1 =
    7    8    3    4    5

That has given another value to the first and second element.

Now, let's extract all the entries of the vector from the second to the last. This can be simply done by typing in:
>> Rvec1([2:end])
ans =
     8     3     4     5

If you need a vector with equally spaced elements, this can be easily done using MATLAB's construct star:increment:finish. If you don't mention an increment, it's assumed to be 1.
>> Rvec3=1:10
Rvec3 =
     1    2    3    4    5    6    7    8    9    10

Further, I will be describing some MATLAB built-in functions that you can use when working with vectors.

The number of elements in a vector is known as the length of a vector.
>> length(Rvec1)
ans =
     5

To generate a specific number of points between two given values, use linspace:
>> linspace(1,2,3)
ans =
    1.0000      1.5000      2.0000

For a logarithmically spaced vector, use logspace:
>> logspace(0,1,5)
ans =
    1.0000    1.7783    3.1623    5.6234    10.0000

To initialise a vector having n elements with 0, use zeros.
>> zeros(1,3)
ans =
    0    0    0

There is a similar function that will initialise all the elements of a vector with 1: ones.
>> ones(3,1)
ans =
     1
     1
     1

Next, I will be working only with  Rvec1  and Cvec1 previously defined, so you might want to clear any other variables existing now in the Workspace.
For that, do the following:

>> clear('Rvec2','Rvec3')

You can notice that those two variables have now been deleted from your Workspace.

The  sort command sorts the elements of the vector is ascending or descending order.
>> sort(Rvec1,'ascend')
ans =
    3    4    5    7    8

>> sort(Cvec1,'descend')
ans =
    5
    4
    3
    2
    1

The mode parameter is optional and is assumed by MATLAB to be  ascend if left blank.

To find the largest or the smallest component, use max, respectively min.
>> max(Cvec1)
ans =
     5

>> min(Rvec1)
ans =
     3

To sum all the elements of a vector, MATLAB has a very suggestively named function: sum
>> sum(Rvec1)
ans =
    27

To compute the product of the elements of a vector, use prod:
>> prod(Cvec1)
ans =
   120

Most of the commands can also be used for matrices since a vector is a matrix that has only one row or column. More details and examples about working with matrices will be found in another post.

For the last section of this article, I will be talking briefly about character arrays in MATLAB.

You can create a string by enclosing a sequence of letters/numbers/special characters in single quotation marks. Here's an example:
>> str1='Vintage'
str1 =
Vintage

To convert characters to their ASCII code or to find their numerical equivalent, use unicode2native:
>> str2=unicode2native(str1)
str2 =
     86     105     110     116     97     103     101

To convert a integer vector to characters use native2unicode or the char function:
>> native2unicode(str2)
ans =
Vintage

>> char(str2)
ans =
Vintage

To determine whether the input is a char array or not, use  ischar. This functions will return 1 if the argument is a string and 0 otherwise.
>> ischar(str1)
ans =
     1

To check if a character is a letter, use isletter. This MATLAB function will return 1 if the character is a letter of the alphabet and 0 otherwise.
>> isletter('*')
ans =
     0

The isspace functions behaves similar and is used to verify spaces in a string.

To compare two strings, use one of the following functions: strcmp (determines if the two input strings are identical), strncmp (determines if the first n characters of the strings are identical), strcmpi, strncmpi. The last two ignore case.
>> strcmp(str1,'vintage')
ans =
     0

>> strcmpi(str1,'vintage')
ans =
     1

Also, all the relational operators (==, ~=, >, >=, <, <=) can be used to compare arrays with equal dimensions.

To horizontally concatenate two or more strings, you can use the strcat function.
>> s1=strcat(str1,'Coding')
s1 =
VintageCoding

For a vertical concatenation, MATLAB has the strvcat function that will return a column vector containing string elements.
>> strvcat(str1,'Coding')
ans =
Vintage
Coding

You can search any string for any occurrence of another string by making use of the findstr function. This will return the starting index of each appearance of the shorter string in the longer one. This search is case sensitive. 
>> findstr('i', 'Vintage Coding')
ans =
    2 12

For a search where the order of the input character arrays is important, use strfind.

Remember that for more details about each of these functions, you can see the help MATLAB provides.

For vector operations in MATLAB, see my next article.

Monday, 8 October 2012

Vector operations in MATLAB

In my most recent article I presented you a large list of commands that will ease your work with vectors in MATLAB.

Now, it's time to discuss about some operations that can be performed on vectors.

I will start by defining the following two vectors:
>> vec1=[1 2 3 4]
vec1 =
     1     2     3     4
>> vec2=[4 3 2 1]
vec2 =
     4     3     2     1

To sum two vectors, they must have the same dimension. Summing two vectors means to sum the elements of the two vectors found on the same position. In order to do this, the vectors must have the same dimension.
>> vec1+vec2
ans =
    5    5    5    5

The observations I made on suming two vectors in MATLAB also apply to substraction.
>> vec1-vec2
ans =
    -3    -1    1    3

Since a row vector is a 1xn matrix and a column vector is a nx1, to multiply vectors, the number of rows of the first one must have the same dimension as the number of columns of the second one.
>> vec1*[1;2;3;4]
ans =
    30

Array operations are done element by element. In order to differentiate them from the matrix operations, the . character is used before the arithmetical operator. However, this character it is not used for addition and substraction since they are the same for both matrices and arrays.

To calculate a element by element operation between two vectors, they both must be either a row or column vector with the same length.

For a element by element multiplication, type in: 

>> vec1.*vec2
ans =
    4    6    6    4

The following sequence computes a element by element division:
>> [1;2;3]./[3;2;1]
ans =
    0.3333
    1.0000
    3.0000

An array raise to power can be performed as follows:
>> vec1.^vec2
ans =
    1    8    9    4

The second vector can also be a scalar:
>> vec1.^2
ans =
    1    4    9    16

For a cross product of two vectors, use cross The two vectors must have the size 3. 
>> v1=[1 2 3]
v1 =
    1     2     3
>> v2=[4 5 6]
v2 =
    4     5     6
>> cross(v1,v2)
ans =
   -3     6     -3

For the scalar product of two vectors, use dot.
>> dot(v1,v2)
ans =
   32

My next post will be about MATLAB commands when working with matrices.