MATLAB Arrays & Vectors

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
 MATLAB Arrays & Vectors All MATLAB Articles Cell Arrays in MATLAB In order to prevent spam, users must register before they can edit or create articles.

## 1 Introdution to Arrays

MATLAB stands for MATrix LABoratory because matrices are at the heart of the computations. Matrices are represented by the data structure array. Therefore, arrays are the fundamental data type of MATLAB.

Vectors are a special case of arrays with only 1 dimension.

## 2 Declaring Arrays

### 2.1 Vectors as Row and Column Arrays

Row vectors can be declared in this manner.

``` >> row_vector = [0 1 2 3]
row_vector =
0      1      2      3
```
or
``` >> row_vector = [0, 1, 2, 3];
```

These two methods are equivalent because MATLAB will assume a row vector and treat the spaces as a separator. However, the comma separated list is easier for people read and helps insure that elements are properly separated in more complex declarations.

To make a column array use semicolons to separate the values.

``` >> column_vector = [1; 2; 3]
column_vector =
1
2
3
```

All elements of an array must be the same data type. In order to mix and match data types, such as numbers and strings, use a cell array.

### 2.2 Declaring multi-dimensional arrays

Arrays can be multi-dimensional. To create a 2 dimensional array (a matrix in Linear Algebra terms), use both commas and semicolons.

``` >> matrix = [1, 2, 3; 4, 5, 6]
matrix =
1     2     3
4     5     6
```
or
``` >> matrix = [1, 2, 3; ...			% The ... operator allows for multi-line input
4, 5, 6]
matrix =
1     2     3
4     5     6
```

Matrices are just 2 dimensional arrays. Given MATLAB's history, arrays are usually treated as a matrix. It should be noted that a matrix, as its mathematical equivalent, requires all its rows and all its columns to be the same size.

## 3 Array Operations

### 3.1 Elements Wise Operations with the dot operator

``` >> [1, 2, 3] + [4, 5, 6]
ans =
5     7     9
```
• Multiplication/Division
``` >> [1, 2, 3] * [4 ; 5 ; 6]   % Row vector times column vector
ans =
32
>> [1, 2, 3] .* [4, 5, 6]    % Element wise multiplication of column vectors
ans =
4    10    18
```
• This returns an error because the array dimensions are not the same ([1, 3] vs [3, 1]).
``` >> [1, 2, 3] .* [4 ; 5 ; 6]
```
• This does not return an error because the ' is the transpose operator. [4 ; 5 ; 6]' = [4, 5, 6]
``` >> [1, 2, 3] .* [4 ; 5 ; 6]'
ans =
4    10    18
```

### 3.2 Matrix Operations

``` >> a = rand(3, 2)
a =
0.8147    0.9134
0.9058    0.6324
0.1270    0.0975
>> b = rand(3, 2)
b =
0.9572    0.1419
0.4854    0.4218
0.8003    0.9157
>> a + b
ans =
1.7719    1.0553
1.3912    1.0541
0.9273    1.0133
>> a - b
ans =
-0.1424    0.7715
0.4204    0.2106
-0.6733   -0.8182
```
• Multiplication, since a and b are both 3 rows and 2 columns (3x2) one of the matrices must be transposed so that the inner dimensions are equal.
``` >> a * b'
ans =
0.9094    0.7807    1.4884
0.9567    0.7064    1.3040
0.1354    0.1028    0.1909
```
• Division, the \ operator uses the pseudoinverse to compute the a divided by b. If b is square then a/b is approximately equal to inv(a)*b.
``` >> a \ b
ans =
-0.3132    1.1935
1.3474   -0.8852
```

## 4 Useful Properties

There are several useful properties that can be retrieved for arrays.

• The size command retrieves the size of each dimension of the array.
``` >> array = [0, 1, 4, 5];
>> size(array)
ans =
1     4
```
• To get the size of the largest dimension use length (length = max(size(array))).
``` >> length(array)
ans =
4
```
• The call of the array can be retrieved using the class command.
``` >> class(array)
ans =
double
```
• A vector magnitude can be determiend using the norm command.
``` >> norm([3 4])
ans =
5
```
• The dot and cross products can be calculated using the dot and cross commands respectively.

## 5 Declaring a vector with linear or logarithmic spacing

In controls it is often useful (especially for bode plots) to have a frequency vector which is evenly spaced across some frequency range. The logspace function generates logarithmically spaced vectors. Especially useful for creating frequency vectors, it is a logarithmic equivalent of linspace and the ":" or colon operator.

• y = logspace(a, b, n)
• Creates a logarithmically spaced row vector with n points between 10^a and 10^b.
• y = linspace(a, b, n)
• Creates a linearly spaced row vector with n points between a and b.

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 defaults to n = 100 elements.

## 6 Accessing elements of a matrix

Create an array of random variables with 3 rows and 2 columns (3x2).

``` >> a = rand(3, 2)
a =
0.8147    0.9134
0.9058    0.6324
0.1270    0.0975
```

To index the ith row and jth column element using a(i, j).

``` >> i = 2; j = 2;
>> a(i, j)
ans =
0.6324
```

To index all values in a column use the : operator.

``` >> a(:, 1)
ans =
0.8147
0.9058
0.1270
```

Indexing all values in a row is similar.

``` >> a(1, :)
ans =
0.8147    0.9134
```

The end operator is useful for accessing part of a column or row so long as you want the last element in that dimension.

``` >> a(2:end, 1)
ans =
0.9058
0.1270
```

To get a vector of indices into an array meeting certain criteria use the find command.

``` >> a = rand(3, 2)
a =
0.8147    0.9134
0.9058    0.6324
0.1270    0.0975
>> ind = find(a(:, 1) > 0.5)
ans =
1
2
>> a(ind, 1)
ans =
0.8147
0.9058
>> a(a(:, 1) > 0.5, 1)
ans =
0.8147
0.9058
```

## 7 Boolean Operators on Arrays

The same boolean operators that can be used for scalar values can also be used to compare arrays. To do this, MATLAB compares each element and returns the result in a logical array of the same size as the two arrays being compared. The two arrays must have the same size.

``` >> a = [2, 4]; b = [1, 5];
>> a < b
ans =
[0    1]
```