Lecture 2 Scip, Matrix handling HA, 24.10.2018

Contents

Extracring and decomposing (parts of) matrices

clear   % Remove variables from workspace (memory)
clc     % Clear screen
close all  % Close grapics windows
format compact    % Compress away extra blank lines from results.
 A=reshape(1:6,2,3),B=ones(2,2),C=diag(1:3)
A =
     1     3     5
     2     4     6
B =
     1     1
     1     1
C =
     1     0     0
     0     2     0
     0     0     3

Side by side

 side_by_side=[A B]
 % Stack vertically:
 pile =  [A;C]
side_by_side =
     1     3     5     1     1
     2     4     6     1     1
pile =
     1     3     5
     2     4     6
     1     0     0
     0     2     0
     0     0     3

Link to indexing (works after "publish")

Lue lisaa lyhyesta

Some Finish explanation of lofical indexing (can ignore now)

Indexing vectors with vector indices

clear
close all
format compact
clc
v = [13 5 9  -1]
v =
    13     5     9    -1
v(2)              % 2. element         -> 5
ans =
     5
v([1 3 end])      % elements 1 3 end   -> 13 9 -1
ans =
    13     9    -1
v(1:3)            % elements 1 2 3     -> 13 5 9
ans =
    13     5     9
v(1:3)=-[1 2 3]   % Update part chosen part of vector.
%                 % Same size or scalar
v =
    -1    -2    -3    -1
v([1 1 end-1 2 1]) % Repetition and changing order allowed.
ans =
    -1    -1    -3    -2    -1
v([1 3])=NaN       % A form of scalar expansion
                   % NaN  -2.0000   NaN  -1
v =
   NaN    -2   NaN    -1

Indexing matrices

clc
clear
format compact
A=[3 33;9 8]
A =
     3    33
     9     8
A(1,1)        % --> 3
A(1,2)        % --> 33
ans =
     3
ans =
    33

Linear indexing, "slicing" matrix into a long column A(:)

%{
  Can index with one index running along columns.
  This is called linear indexing.
  Think of A "queued" along columns, i.e. A(:)
%}

A(3)    % <--> A(2,1)
[A(3) A(1,2)]
sub2ind([2,2],1,2)
%
ans =
    33
ans =
    33    33
ans =
     3

Some more cases of indexing matrices:

A = magic(6)
A =
    35     1     6    26    19    24
     3    32     7    21    23    25
    31     9     2    22    27    20
     8    28    33    17    10    15
    30     5    34    12    14    16
     4    36    29    13    18    11
B = A(3,5)
B =
    27
C = A([1,2,3],4)        % Sarakkeen 4 alkiot riveilta 1 2 3
C =
    26
    21
    22
D = A(4,[1,1,1])        % [A(4,1) A(4,1) A(4,1)]
D =
     8     8     8
E = A([2,5],[3,1])      % Rivien 2 5 sarakkeet 3 1
E =
     7     3
    34    30
F = A(:,4)              % Koko 4. sarake
F =
    26
    21
    22
    17
    12
    13
G = A(4,:)              % Koko 4. rivi
G =
     8    28    33    17    10    15
H = A(:)               % Columns of A "sliced" into one long column:
size(H)
H =
    35
     3
    31
     8
    30
     4
     1
    32
     9
    28
     5
    36
     6
     7
     2
    33
    34
    29
    26
    21
    22
    17
    12
    13
    19
    23
    27
    10
    14
    18
    24
    25
    20
    15
    16
    11
ans =
    36     1
H'                      % Show transposed to save display space.
A(1:3,[2 3 end-1])=NaN  % Update as before with vectors.
%
ans =
  Columns 1 through 13
    35     3    31     8    30     4     1    32     9    28     5    36     6
  Columns 14 through 26
     7     2    33    34    29    26    21    22    17    12    13    19    23
  Columns 27 through 36
    27    10    14    18    24    25    20    15    16    11
A =
    35   NaN   NaN    26   NaN    24
     3   NaN   NaN    21   NaN    25
    31   NaN   NaN    22   NaN    20
     8    28    33    17    10    15
    30     5    34    12    14    16
     4    36    29    13    18    11