Difference between revisions of "Matlab Arrays"
m (→Other Useful Tools) |
|||
Line 37: | Line 37: | ||
Here the <math>^\mathsf{T}</math> superscript indicates transpose. Note that if we transpose an array twice, we recover the original array. In other words, <math>\left(A^\mathsf{T}\right)^\mathsf{T} = A</math>. | Here the <math>^\mathsf{T}</math> superscript indicates transpose. Note that if we transpose an array twice, we recover the original array. In other words, <math>\left(A^\mathsf{T}\right)^\mathsf{T} = A</math>. | ||
− | == Creating | + | == Creating Arrays in Matlab == |
− | === | + | === Manual Creation === |
− | Often we need to create small | + | Often we need to create small arrays with only a few numbers. In this case, it is easiest to create the vector in one Matlab statement. For example, to create the vector |
<math> | <math> | ||
a = \left[ \begin{array}{cc} 2 \\ 4 \\ 6 \\ 8 \end{array} \right] | a = \left[ \begin{array}{cc} 2 \\ 4 \\ 6 \\ 8 \end{array} \right] | ||
Line 57: | Line 57: | ||
b = [ 1; 3; 5; 7 ]'; % transpose a column vector. | b = [ 1; 3; 5; 7 ]'; % transpose a column vector. | ||
− | Note that the semicolon (;) | + | Note that the semicolon (;) identifies the end of a row in the matrix. Commas may be used to separate columns in the matrix, but they are not necessary. |
+ | |||
+ | Matrices may be created directly in much the same way as vectors are. As an example, let's build the matrix <math>A = \left[ \begin{array}{cc} 2 & 4 \\ 6 & 8 \\ 10 & 12 \end{array} \right]</math>. This may be done in several ways. | ||
+ | |||
+ | A = [ 2 4; 6 8; 10 12 ]; | ||
+ | A = [ 2 4 | ||
+ | 6 8 | ||
+ | 10 12 ]; | ||
+ | Note that a semicolon (;) separates each row of the matrix while spaces separate each column. | ||
+ | |||
+ | We may also create a matrix from vectors. For example, we may construct '''A''' from its rows as follows: | ||
+ | arow1 = [ 2 4 ]; | ||
+ | arow2 = [ 6 8 ]; | ||
+ | arow3 = [ 10 12 ]; | ||
+ | A = [ arow1; arow2; arow3 ]; | ||
+ | |||
+ | Similarly, we can build a matrix from its columns: | ||
+ | acol1 = [2 6 10]'; | ||
+ | acol2 = [4; 8; 12]; | ||
+ | A = [ acol1 acol2 ]; | ||
+ | |||
Line 98: | Line 118: | ||
c = logspace(-1,3,5)'; % creates a column vector | c = logspace(-1,3,5)'; % creates a column vector | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Line 127: | Line 126: | ||
* '''rand'''(nrow,ncol) | * '''rand'''(nrow,ncol) | ||
+ | |||
=== Sparse Matrices === | === Sparse Matrices === | ||
+ | |||
+ | |||
== Accessing Arrays == | == Accessing Arrays == |
Revision as of 08:11, 29 July 2008
Array Basics
An array is an n-dimensional collection of numbers. Matrices are 2-dimensional arrays, and vectors are 1-dimensional arrays.
Vectors can be either a row or column vector:
We refer to the elements of an array by their position in the array. For example, the third element in is 3.2, and the second element in is .
Matrices are two-dimensional arrays, whose elements are referred to by the row and column that they belong to. For example,
is a matrix with three rows and two columns. We refer to this as a matrix. The first index is the row and the second index is the column.
Transposing Arrays
Occasionally we want to transpose an array. This is done by exchanging rows and columns. For example,
Here the superscript indicates transpose. Note that if we transpose an array twice, we recover the original array. In other words, .
Creating Arrays in Matlab
Manual Creation
Often we need to create small arrays with only a few numbers. In this case, it is easiest to create the vector in one Matlab statement. For example, to create the vector we could use any of the following Matlab commands:
a = [ 2; 4; 6; 8; ]; % create a column vector a = [ 2 4 6 8 ]'; % create a row vector and then % transpose it to produce a column vector. a = [ 2 4 6 8 ]; % creates a column vector.
Similarly, a row vector such as b = [1 3 5 7 ] may be created as
b = [ 1 3 5 7 ]; b = [ 1, 3, 5, 7 ]; b = [ 1; 3; 5; 7 ]'; % transpose a column vector.
Note that the semicolon (;) identifies the end of a row in the matrix. Commas may be used to separate columns in the matrix, but they are not necessary.
Matrices may be created directly in much the same way as vectors are. As an example, let's build the matrix . This may be done in several ways.
A = [ 2 4; 6 8; 10 12 ]; A = [ 2 4 6 8 10 12 ];
Note that a semicolon (;) separates each row of the matrix while spaces separate each column.
We may also create a matrix from vectors. For example, we may construct A from its rows as follows:
arow1 = [ 2 4 ]; arow2 = [ 6 8 ]; arow3 = [ 10 12 ]; A = [ arow1; arow2; arow3 ];
Similarly, we can build a matrix from its columns:
acol1 = [2 6 10]'; acol2 = [4; 8; 12]; A = [ acol1 acol2 ];
Using the ":" operator to create arrays
Sometimes we have vectors that have specific patterns. For example, a=[1 2 3 4 5]. The : operator can help us create such arrays:
array = lower : increment : upper;
This creates an array starting at lower and ending at upper with spacing of increment. You may eliminate the increment argument, in which case it defaults to 1.
Examples:
a = 1:5; % creates a=[ 1 2 3 4 5 ] b = 1:1:5; % creates b=[ 1 2 3 4 5 ] c = 1:2:7; % creates c=[ 1 3 5 7 ] d = 1:2:8; % creates d=[ 1 3 5 7 ] e = 1.1:5 % creates e=[ 1.1 2.3 3.1 4.1 ] f = 0.1:0.1:0.5 % creates f=[ 0.1 0.2 0.3 0.4 0.5 ] g = 5:-1:1; % creates g=[ 5 4 3 2 1 ]; h = (1:3)'; % creates a column vector with elements 1, 2, 3.
Linspace and Logspace
Occasionally we want to create numbers that are equally spaced between two limits. We can use the linspace command to do this.
linspace( lo, hi, n ); Creates a row vector with n points equally spaced between lo and hi.
Examples:
a = linspace(1,5,5); % creates a=[ 1 2 3 4 5 ] b = linspace(1,5,4); % creates b=[ 1.0 2.333 3.667 5.0 ] c = linspace(5,1,5); % creates c=[ 5 4 3 2 1 ] d = linspace(1,5,5)'; % creates a column vector
Sometimes we want points logarithmically spaced. For example, we may want to create a vector a=[ 0.1 1 10 100]. This can be accomplished using the logspace command:
logspace( lo, hi, n ); Creates a vector with n points spaced logarithmically between and
Examples:
a = logspace(-1,3,5); % creates a=[ 0.1 1 10 100 1000 ] b = logspace(3,-1,5); % creates b=[ 1000 100 10 1 0.1 ] c = logspace(-1,3,5)'; % creates a column vector
Shortcuts for Special Matrices
- ones(nrow,ncol)
- zeros(nrow,ncol)
- eye(nrow,ncol)
- rand(nrow,ncol)
Sparse Matrices
Accessing Arrays
Indexing arrays
Slicing arrays using the ":" operator
Information about Arrays
- size
- length
- max
- min
Other Useful Tools
- sum
- sort
- any
- all
- find
- isequal