Beruflich Dokumente
Kultur Dokumente
UC Berkeley
Fall 2004, E77
http://jagger.me.berkeley.edu/~pack/e77
Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike
License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to
Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
Arrays
Row and Column vectors are also sometimes just called vectors
zeros(n,m)
a n-by-m double array, each entry is equal to 0
rand(n,m)
a n-by-m double array, each entry is a random number between 0
and 1.
Examples
>> A = ones(2,3);
>> B = zeros(3,4);
>> C = rand(2,5);
: convention
The : (colon) convention is used to create row vectors, whose
entries are evenly spaced.
If A is an array, then
size(A,1) is the number of rows of A
size(A,2) is the number of columns of A
Example
>> A = rand(5,6);
>> B = size(A)
>> size(A,2)
Accessing single elements of a vector
If A is a vector (ie, a row or column vector), then
A(1) is its first element,
A(2) is its second element,
Example
>> A = [ 3 4.2 -7 10.1 0.4 -3.5 ];
>> A(3)
>> Index = 5;
>> A(Index)
This syntax can be used to assign an entry of A. Recall assignment
>> VariableName = Expression
An entry of an array may also be assigned
>> VariableName(Index) = Expression
So, change the 4th entry of A to the natural logarithm of 3.
>> A(4) = log(3);
Accessing multiple elements of a vector
A(3) refers to the 3rd entry of A. However, the index need not
be a single number.
In an assignment
>> M(RIndex,CIndex) = Expression
the right-hand side expression should be a scalar, or the same
size as the array being referenced by M(RIndex,CIndex)
Do some examples
Layout in memory
The entries of a numeric array in Matlab are stored
together in memory in a specific order.
>> A = [ 3 4.2 8 ; -6.7 12 0.75 ];
represents the array
Name = A
3.4 4.2 8
6.7 12 0.75 Size = [2 3];
Data = 3
Somewhere in memory, Matlab has -6.7
4.2
12
8
0.75
Workspace: Base
RESHAPE
RESHAPE changes the size, but not the values or order of the data in
memory.
>> A = [ 3 4.2 8 ; -6.7 12 0.75 ];
>> B = reshape(A,[3 2]);
The result (in memory is)
So, A is the array
Name = A Name = B
3.4 4.2 8
Size = [2 3]; Size = [3 2]; 6.7 12 0.75
Data = 3 Data = 3
-6.7 -6.7 while B is the array
4.2 4.2
12 12 3 12
8 8 6.7 8
0.75 0.75 4.2 0.75
END
Suppose A is an N-by-M array, and a reference of the form
A(RIndex,CIndex)
Example:
>> M = rand(4,5);
>> M(:,[1 3 5])
LINSPACE and LOGSPACE
>> linspace(A,B,N) is a 1-by-N row vector of evenly
spaced numbers, starting at A, and ending at B
Examples
>> linspace(1,4,6)
>> logspace(-1,1,5)
>> log10(logspace(-1,1,5))
Unary Numeric Operations on double Arrays
Unary operations involve one input argument. Examples are:
Negation, using the minus sign
Trig functions, sin, cos, tan, asin, acos, atan,
General rounding functions, floor, ceil, fix, round
Exponential and logs, exp, log, log10, sqrt
Complex, abs, angle, real, imag
By default, Matlab will draw straight lines between the data points, and the
points will not be explicitly marked. For more info, do >> help plot
Example:
>> X = linspace(0,3*pi,1000);
>> Y = sin(X);
>> plot(X,Y)
Plotting several lines
If X1 and Y1 are both 1-by-N, and X2 and Y2 are both 1-
by-M, then
>> plot(X1,Y1,X2,Y2)
will plot both sets of data on the same axis.
Example
>> X1 = linspace(0,pi,1000);
>> Y1 = cos(4*X1).*sin(X1);
>> X2 = [0 1 4 5];
>> plot(X1,Y1,X2,sqrt(X2))