Professional Documents
Culture Documents
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
A rectangular arrangement of numbers is called
an array.
This is a 4-by-2 array.
It has 4 rows, and 2
columns.
The (3,1) entry is the
entry in the 3rd row,
and 1st column
Creating Arrays
Horizontal and Vertical Concatenation (ie., stacking)
Square brackets, [, and ] to define arrays
Spaces (and/or commas) to separate columns
Semi-colons to separate rows
Example
3 4 5
>> [ 3 4 5 ; 6 7 8 ] is the 2-by-3 array 6 7 8
[4 5;7 8] ]
is equal to [ 3 4 5;6 7 8 ]
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.
7:2:18 equals the row vector
[ 7 9 11 13 15 17 ]
If A is an array, then
Example
>>
>>
>>
>>
A = [ 3 4.2
A(3)
Index = 5;
A(Index)
-7
10.1
0.4
-3.5 ];
In an assignment
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
-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]);
Name = A
Name = B
Size = [2 3];
Size = [3 2];
Data = 3
Data = 3
-6.7
-6.7
4.2
4.2
12
12
0.75
0.75
3.4 4.2
8
6.7 12 0.75
6.7
12
8
4.2 0.75
END
Suppose A is an N-by-M array, and a reference of the form
A(RIndex,CIndex)
Any occurence of the word end in the RIndex is changed
(automatically) to N
Any occurence of the word end in the CIndex is changed
(automatically) to M
Example:
>> M = rand(4,5);
>> M(end,end)
>> M([1 end],[end-2:end])
A(RIndex,CIndex)
If RIndex is a single colon, :, then RIndex is changed
(automatically) to 1:N (every row)
If CIndex is a single colon, :, then CIndex is changed
(automatically) to 1:M (every column)
Example:
>> M = rand(4,5);
>> M(:,[1 3 5])
>> linspace(1,4,6)
>> logspace(-1,1,5)
>> log10(logspace(-1,1,5))
B = sin(A);
is an N1-by-N2-by-N3-by- array. Every entry of B is
the sin of the corresponding entry of A. The for-loop that
Scalar-Array Multiplication
If A is an array,and B is a scalar, then A*B is an array of the same size as A,
whose individual entries are the product of the corresponding entries of A and
the scalar B.
Element-by-Element Multiplication
If A and B are arrays of the same size, then A.*B is an array of the same size
whose individual entries are the product of the corresponding entries of A and B
Matrix multiplication
If A and B are arrays, then A*B is the matrix multiplication of the two arrays
More later
>> plot(X,Y)
creates a figure window, and plots the data in the axis. The points plotted
are
>> X = linspace(0,3*pi,1000);
>> Y = sin(X);
>> plot(X,Y)
>> 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))