% vectors_and_matrices
% This script demonstrates how to generate and use vectors and matrices in
% Matlab.
% It is part of the Matlab course for PhD Students 2010-2011:
% http://www.sinnesphysiologie.uni-oldenburg.de/50246.html
% written by Jutta Kretzberg, 15.11.2010


%% Generation of vectors and matrices in Matlab
% Matrices and vectors are generated in Matlab either by typing in the 
% values of their elements, or by using specific functions. 

s=7                     % generates a variable s with scalar value
                        % Dimensions of s are 1-by-1
vr=[7 -9 8.8]           % generates a row-vector vr 
                        % (dimensions 1-by-3).
                        % square brackets [ ] generally generate 
                        % vectors and matrices.  
vr2=[8,-55,9.7]         % also generates a 1-by 3 row-vector vr2 
                        % columns in a vector or matrix can be 
                        % separated either by a space or by a comma
vc=[0.5; -4; 0]         % generates a 3-by-1 column vector. 
                        % rows in a vector or matrix are separated 
                        % by a semicolon
M=[0 5; 0.1 7; -9 8.2]	% generates a 3-by-2 matrix 
L=[]                    % generates an empty variable L that can later 
                        % be filled with values 
C=1:7                   % generates a vector with consecutive numbers 
                        % from 1 to 7 (C=[1 2 3 4 5 6 7])
C2=0:3:10               % generates a vector with numbers from 0 to 10
                        % in steps of 3 (C2=[0 3 6 9])
C3=1:-0.1:0.6           % generates a vector with numbers from 1 to
                        % 0.5 in steps of -0.1 (C3=[1 0.9 0.8 0.7 0.6])
y=linspace(37,95,17)    % generates a row vector y of 17 points linearly 
                        % spaced between and including 37 and 95.
                        % (Can be used alternatively to : if you know the
                        % number of elements rather than the difference
                        % between the values.)
Z=zeros(7,3)            % generates a 7-by-3 matrix with all elements 0
O=ones(1,5)             % generates a 1-by-5 vector with all elements 1
R1=rand(4,2)            % generates a 4-by-2 matrix with random
                        % elements, values are equally distributed 
                        % between 0 and 1 
R2=randn(4,2)           % generates a 4-by-2 matrix with random
                        % elements, values are normally distributed 
                        % with mean 0 and standard deviation 1 
p=randperm(7)           % generates a vector with all numbers from 1 to
                        % 7 in random order. 

%% Indexing in Matlab 
% You need indexing to address specific elements of a vector or a matrix to 
% either read their values (e.g. to copy them to a new variable) or to 
% change their values in the matrix or vector. 

el1=vr2(2)              % assigns the second element of vector vr2 to 
                        % the variable el1 (for vr2=[8,-55,9.7] you get 
                        % el1=-55)
vr2(1)=0                % assigns the value 0 to the first element of 
                        % vector vr2 (vr2=[8,-55,9.7] changes to 
                        % vr2=[0,-55,9.7])
el2=M(2,1)              % assigns the element in the second row and 
                        % first column of matrix M to the variable el2
                        % (for M=[0 5; 0.1 7; -9 8.2] you get el2=0.1)
M(3,2)=66               % assigns the value 66 to the element in row 3
                        % and column 2 of Matrix M. 
                        % (M=[0 5; 0.1 7; -9 8.2] changes to 
                        % M=[0 5; 0.1 7; -9 66])
el3=M(4)                % Matrices can also be indexed linearly with 
                        % only one index! Elements are counted
                        % column-wise, continuing after all elements of 
                        % the first column with the first element of  the 
                        % second column
                        % (for M=[0 5; 0.1 7; -9 66] you get el3=5)
l=C(end)                % end is the position of the last element in a 
                        % vector or matrix. (for C=[1 2 3 4 5 6 7] is l=7)
Cpart=C3([2,5])         % Vectors can be used for indexing of several 
                        % elements of a vector or matrix. (For 
                        % C3=[1 0.9 0.8 0.7 0.6] you get Cpart=[0.9 0.6])
C3([2,5])=[7,9]         % Vectors used for indexing and replacing
                        % several elements. (For C3=[1 0.9 0.8 0.7 0.6]
                        % you get C3=[1 7 0.8 0.7 9])
Cshort=C(4:6)           % You can also use the : to generate a vector of 
                        % indices. 
C(1:3)=0                % a scalar value after the = replaces all indexed 
                        % elements with this value (C=[1 2 3 4 5 6 7] 
                        % becomes C=[0 0 0 4 5 6 7])
last4=M(end-3:end)      % end can also be used to generate vectors
                        % of indices. (For M=[0 5; 0.1 7; -9 66] you get 
                        % last4=[-9 5 7 66])
column1=M(:,1)          % the : alone means that the entire dimension is 
                        % taken. Here, all elements of the first column of
                        % matrix M are copied into the column vector
                        % column1
long_vec=M(:)           % a single : applied as index to a matrix takes all 
                        % elements of M in linear order (down the columns). 
                        % For the 3-by-2 matrix M the result long_vec is a
                        % column vector with 6 elements.
                        
%% Determine the size of vectors and matrices
% It is often important to determine the size of a data set, e.g. to make
% sure to apply a calculation to all elements.

l=length(vc)            % the function length gives back the number of 
                        % elements in vector vc to its output variable l
l2=length(M)            % If length is applied to a matrix, it gives back
                        % the length of the longest dimension.
r=size(M,1)             % size(M,1) determines the length of the first 
                        % dimension of M, the rows, and gives it back to
                        % its output variable r
c=size(M,2)             % length of the second dimension, the columns, is 
                        % written to variable c
[r2,c2]=size(M)         % If size is used without specifying the 
                        % dimension, it gives back the length of both 
                        % dimensions. The length of the rows is written to
                        % the first output argument r2, the length of the
                        % columns to the second c2.

%% Concatenation
% In Matlab, vectors and matrices can be combined with square brackets [ ].
% However, since all rows and all columns of a matrix must have the same 
% length, concatenation is only possible if it does not break this rule. 

vec_long=[[0 1 2], Cshort]   % square brackets concatenate vectors to a 
                            % long vector (Cshort is a row vector)
vec_long=[vec_long, 1:5, last4] % the vector vec_long is made longer by
                            % concatenating itself and two other vectors
mat1=[[0 1 2]; Cshort]      % vectors of equal length can be concatenated 
                            % to a matrix.                             
mat2=[vec_long(1:3);vec_long(4:6)]   % you can re-combine parts of vectors 
                            % and matrices. (mat2 is equal to mat1)
square=[mat2;[0.5 7 -0.5]]   % vectors and matrices can be mixed, as long as
                            % their dimensions match
square=[square(2,:);square(3,:);square(1,:)]  % re-sorts the rows of the matrix                            

%% Transposing
% Transposing turns a row-vector into a column vector and vice versa. 
% In matrices, the indices of rows and columns are interchanged. 
% In square matrices this means that the matrix is mirrored across the 
% diagonal. 

C_trans=C'              % transposing with ' makes a column vector out of 
                        % a row vector
C_again=C_trans'        % ... and the other way round
square_trans=square'    % transpose a square matrix
mat1=mat1'              % replace e.g. a 2-by-3 matrix with a 3-by-2 matrix

%% Reshaping
 % Reshaping ? changing the dimensions of  matrix and thereby the order of
 % the matrix elements ? can be done either ?by hand? by using indexing and 
 % concatenating the matrix elements in the right order (sometimes also 
 % transposing is needed) or by using the reshape command. 
 
 
vec2=mat2(:)                % reshapes the 2-by-3 matrix mat2 to a column 
                            % vector
vec2a=reshape(mat2,6,1)     % produces the same column vector with reshape                           
M_new=reshape(mat2,3,2)     % reshapes the 2-by-3 matrix mat2 into a 3-by-2
                            % matrix M_new using linear indexing (picking
                            % the elements down the columns)
M_newa=[mat2(1:2);mat2(3:4);mat2(5:6)] % also generates a 3-by-2 matrix, but 
                                        % elements are in a different order
                            

                            


