1xN or Nx1 Array
Mathematically - a collection of variables describing a feature/object
% Row Vector
=[1 2 3 4]
Vsize(V)
% Column
=[1; 2; 3; 4]
Vsize(V)
% ROW
=1:4
V
% Column
=1:4' %' is transpose
V
%Also technically a vector
=1
Vsize(V)
1:9 % Range
1:2:9 % Increment by 2
1:2:8 % Non inclusive
=linspace(1,9,10) % 10 values equally spaced between 1 and L
load('myfunctions')
=linspace(-5,5,1000)
x=f(x)
yplot(x,y);
xlabel('x values')
ylabel('y values')
title('function f')
Grids
Say we want a 2D version of linspace for 3D functions
We need to evaluate at both x and y
=linspace(1,9,10) % 10 values equally spaced between 1 and 9
Lplot(L,L,'.') % This won't work
,Y]=meshgrid(L); % This distributes x and y
[Xplot(X,Y,'.k') % This works.
We will return to this after we look at some 2D plots, but first strings ->
NxM
Mathematically - a linear "operator" composed of rotations and scaling
Practically - can be thought of as a collection of vectors
=[1 4 7 10; 2 5 8 11; 3 6 9 12];
V
=[1 4 7 10; ...
V2 5 8 11; ...
3 6 9 12]
=1:12'
V=resize(V,3,4)
Vsize(V)
=rand(3,4) V
NxMxO Array and Arrays beyond 3 dimensions
=rand(3,4,5,4)
V
=rand(3,4)
V=resize(V,1,3,4) V
Array of char
='t' % Character to variable
d='This is a string' % String to variable
c% what if I didn't use quotes? what would that mean?
=['T' 'h' 'i' 's']
c=['This' 'is' 'my string']
c=['This is my string']
c=['t'; 'h'; 'i'] % don't work well as column vectors
c=['t'; 'h'; 'i']' c
=rand(1000); A
Where is the symbol A located in memory?
What is A bound too?
Where is the matrix data located?
=A; B
What is happening?
=B+1; B
Copy on eval
B is an alias for A until we modify A or B
Python
import numpy
=numpy.random.rand(100)
A=A
B=A*3
A
A B
R and Numpy mimics matlab matrix memory management
Manual array construction
=[1 2 3]
A=[7/2 11/3 3/2] % A row vector (1x3 matrix)
A=[7/2, 11/3, 3/2] % Also a row vector
B=[7/2; 11/3; 3/2] % Column vector (3x1 matrix)
C=[7/2 11/3 3/2; 3 2 1; 3, 2, 1] % A 3x3 matrix A
New column with comma and/or space
Either a comma or space is required, or can have both
Functions and operators are designed to work on matrices
=[7/2 11/3 3/2]
A=[3 2 1]
B+B
A-B A
Matrix operations vs element wise operations
*B % Invalid
A' % Transpose
A'*B % Inner product, a type of dot product - measures similarity between vectors
A*B' % Outer product
A.*B % Element wise product
A'/B % A matrix multiplied by inverse B
A./B % Element wise division
A'^B % Repeated auto-inner product
A.^B % Element wise exponent A
Functions on arrays
round(A)
size(A)
size(8) % 8 is a 1x1 array!
Combining arrays
=[1 2 3];
A=[4 5 6];
B
[A B]=[7; 8; 9];
C% This doesn't work
[A C] '] % This does [A C
DO EXCERCISE 2
=@(x) x.^2+3 % With 1 input
fsize(A) % also an array
Comparing Arrays
==B
A
isequal(A,B)
='perl'
s1='python'
s2
isequal(s1,s2)
strcmp(s1,s2)
DO EXERCISE 3
Two ways of references elements in a matrix
Indexing - Single value
Subscripts - Two values
Indexing runs down columns then rows:
1 | 4 | 7 |
2 | 5 | 8 |
3 | 6 | 9 |
Subscripts - specifies rows then columns
1,1 | 2,1 | 3,1 |
1,2 | 2,2 | 3,2 |
1,3 | 2,3 | 3,3 |
Either way RC (rows then columns)
=[80 106 32; 90 70 4; 3 1 2]
A1) % First elements
A(1:3) % Elements 1 through 3
A(:) % All elements
A(1:end) % Elements 1 through the last
A(2:end-1) % Second element to the second to last element
A(=A(:) % Matrix to column vector B
<- c(1,2,3)
A 1]
A[1:3]
A[1:length(A)-1] A[
R same, but with square braces and without "end" feature
import numpy
=numpy.array([1, 2, 3])
A1]
A[0]
A[1:]
A[0:3];
A[-1]
A[:0] A[:
python is pretty strange, more on that later
=[80 106 32; 90 70 4; 3 1 2]
A1,3) % Element in first row, third column
A(1:3,1:2) % Elements in 1-3 rows with 1-2 columns
A(1,:) % Everything in the first row
A(2:end-1,:) % Everything but the first row A(
Trick to remember:
Index - Individual
Subscripts - Begins with s - plural
,m]=find(A==3)
[n=sub2ind(sz,sub1,sub2)
ind=ind2sub(sz,ind) ind
subscripting work the same in R and python, but
Length
=rand(10,1)
A=rand(1,10)
B=length(A); % Size of largest dimension
A=length(B); % Size of largest dimension B
Size
size(A) % Size of all dimensions
size(A,1) % Query dimensions
size(A,2)
numel(A) % Number of elements in matrix
sum(A,1) % Add all elements accross rows
sum(A,2) % Add all elements accross columns
sum(A) % Default to sum accross rows (dimension 1), leaving row vector
min(A,[],1) % Minimum values in rows
max(A,[],2) % Maximum values in rows
min(A,B,1) % Minimum values in rows between A & B
min(A,B,2) % Minimum values in columns between A & B
imagesc(A)
colorbar
DO EXCERCISE 8
Editing Matrices
=zeros(10)
A1)=1
A(2:end-1)=0
A(:,1)=rand(10,1); A(
Deleting values
size(A)
1,:)=[]
A(size(A)
1)=[]
A(size(A)
Combining matrices
=ones(10)
B
[A B]=1
a=[2 1]
b1 3 4]]
[a b ['] invalid [a b
DO EXCERCISE 7
Matrix query/Creation functions
These take the following form:
<function>(nRows,nColumns)
OR<function>(nRows/Columns) %if square matrix
Random matrices
rand(3,1) % 3x1 matrix with elements sampled uniform distribution between zero and 1
rand(1,3) % 1x3 matrix
rand(3,3) % 3x3 matrix
rand(3) % 3x3 matrix
Controlling the randomness
3) % rng sets the random seed
rng(rand(5) % Note the value here
rand(5)
3)
rng(rand(5) % The value here should be the same as above
Expanding Rand
times you need some regular matrix, but no function exists
Many function to sample between -30 & 30 exists
No get values between -30 & 30?
How to rand(3,3)*60-30
Tensors
What happens if we do more than 2 dimensions)?
rand(3,3,3) 3D matrix aka tensor
Can't show a 3d matrix at once, so it shows slices
Showing subscripts of each dimension
':' means all elements
Large Outputs and semicolon
rand(100,100,100) % Should produce a large output
<C-c> % Cancels operation
rand(100,100,100); % Semi-colon supresses output
rand(1000,1000,1000); % You should get an error about memory limitations
=rand(100); B=rand(200); % Semicolon can separate commands on same line A
Randi
randi(100,3,2) % 3x2 matrix with random integers between 1 & 100
Other important
ones() % Matrix of just ones
zeros() % Matrix of zeros
eye() % Identity matrix
figure(1)
=randn(1000,1) % Normally distributed random numbers
A% Histogram, automatically setting the number of bins
histogram(A) ,10) % Set number of bins to 10
histogram(Ahelp histogram
figure(5)
=[countsA; countsB]
countsbar(ctrsA,counts')
bar(ctrsA,counts',1)
bar(ctrsA,counts','stacked')
close all % Close all figures
figure(5)
plot(ctrsA,countsB,'k')
hold on
plot(ctrsB,countsB,'r')
close all
DO EXCERSISE 9