playground_02_data_structures - VMinhoto/workshop-matlab GitHub Wiki

Matrixes Stuff

Vectors

Vectors in Matlab can be a row vector or a column vector. A row vector is initialised like this: rowV=[1 2 3] and a column vector like this: colV=[1;2;3]. Note that spaces are used to separate row elements and ; are used to separate column elements. All vectors are seen as a 1 column or 1 row matrix, so all operations are matrix operations.

%Assign a row vector
rowV = [1 2 3 4 5];
%Assign a column vector
colV = [1;2;3;4;5]; 

%Transpose a vector
rowV' %This turns the row vector into a column vector

%Accessing vector element
rowV(3) %ans = 3

%Sub-vector
subRowV=rowV(3:5); % subRowV=[3 4 5];

%Vector with uniformly spaced elements
coolVector=[1:2:10]; %notation [beginning:step:end]
%Can also do
coolVectorTwo=linspace(1,10,5) %notation linspace(beggining, end, numberOfPoints)

Matrix

Matlab sees everything as Matrixes. Here you have some examples

a = [ 11 12 13 14 15; 21 22 23 24 25; 31 32 33 34 35; 41 42 43 44 45; 51 52 53 54 54];
%{
a =

    11    12    13    14    15
    21    22    23    24    25
    31    32    33    34    35
    41    42    43    44    45
    51    52    53    54    55
%}

%Get a matrix element
element=a(1,3); %Syntax: name(row, column). element = 13;
%Get 2nd line of matrix
line=(2,:);
%Get sub-matrix from 2nd to 4th column and from 2nd to 4th column.
subMatrix=(2:4,2:4);

%Eliminate the 5th row of a
a(5,:)=[];
%{
a =

    11    12    13    14    15
    21    22    23    24    25
    31    32    33    34    35
    41    42    43    44    45
%}

Matrix operations are really simple. For some quick reference you can consult the documentation.