Date: 10/03/2018 10:00 AM |
Day: Saturday |
Section Num.: (02) |
Matrices and Arrays
- MATLAB is an abbreviation for "matrix laboratory." While other programming languages mostly work with numbers one at a time, MATLAB® is designed to operate primarily on whole matrices and arrays.
>> a = [1 2 3 4]
a =
1 2 3 4
- This type of array is a row vector.
>> a = [1 2 3; 4 5 6; 7 8 10]
a =
1 2 3
4 5 6
7 8 10
- Another way to create a matrix is to use a function, such as ones, zeros, or rand. Forexample, create a 5-by-1 column vector of zeros.
>> z = zeros(5,1)
z =
0
0
0
0
0
- Matrix and Array Operations
>> a + 10
ans =
11 12 13
14 15 16
17 18 20
>> sin(a)
ans =
0.8415 0.9093 0.1411
-0.7568 -0.9589 -0.2794
0.6570 0.9894 -0.5440
- To transpose a matrix, use a single quote ('):
>> a'
ans =
1 4 7
2 5 8
3 6 10
- You can perform standard matrix multiplication, which computes the inner products between rows and columns, using the * operator. For example, confirm that a matrix times its inverse returns the identity matrix:
>> p = a*inv(a)
p =
1.0000 0 -0.0000
0 1.0000 0
0 0 1.0000
- To perform element-wise multiplication rather than matrix multiplication, use the .* operator:
>> p = a.*a
p =
1 4 9
16 25 36
49 64 100
- The matrix operators for multiplication, division, and power each have a corresponding array operator that operates element-wise. For example, raise each element of a to the third power:
>> a.^3
ans =
1 8 27
64 125 216
343 512 1000
- Concatenation is the process of joining arrays to make larger ones. In fact, you made your first array by concatenating its individual elements. The pair of square brackets [ ] is the concatenation operator.
>> A = [a,a]
A =
1 2 3 1 2 3
4 5 6 4 5 6
7 8 10 7 8 10
- Concatenating arrays next to one another using commas is called horizontal.
>> A = [a; a]
A =
1 2 3
4 5 6
7 8 10
1 2 3
4 5 6
7 8 10
- Array Indexing
>> A = magic(4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
- There are two ways to refer to a particular element in an array. The most common way is to specify row and column subscripts, such as
>> A(4,2)
ans =
14
- Less common “linear indexing”, but sometimes useful, is to use a single subscript that traverses down each column in order:
>> A(8)
ans =
14
- To refer to multiple elements of an array, use the colon operator, which allows you to specify a range of the form start:end. For example, list the elements in the first three rows and the second column of A:
>> A(1:3,2)
ans =
2
11
7
- The colon alone, without start or end values, specifies all of the elements in that dimension. For example, select all the columns in the third row of A:
>> A(3,:)
ans =
9 7 6 12
- The colon operator also allows you to create an equally spaced vector of values using the more general form start:step:end.
>> B = 0:10:100
B =
0 10 20 30 40 50 60 70 80 90 100
- To call a function, such as max, enclose its input arguments in parentheses:
>> A = [1 3 5];
>> max(A)
ans =
5
- If there are multiple input arguments, separate them with commas:
>> B = [10 6 4];
max(A,B)
ans =
10 6 5
- When there are multiple output arguments, enclose them in square brackets:
>> [maxA,location] = max(A)
maxA =
5
location =
3