%% Lecture 2 Scip, Matrix handling  HA, 24.10.2018
%
%% 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)
 %%
 % Side by side
 side_by_side=[A B] 
 % Stack vertically:
 pile =  [A;C] 
%% Link to indexing (works after "publish")
% <http://math.aalto.fi/~apiola/matlab/opas/lyhyt/perusteet.html#osama Lue lisaa lyhyesta>
% 
%%
% 
% <html>
% <a href="http://math.tkk.fi/~apiola/matlab/opas/lyhyt/perusteet.html#loogind">
% Some Finish explanation of lofical indexing </a> (can ignore now)
% </html>
%             
%% Indexing vectors with vector indices
%
clear
close all
format compact
clc
%%
v = [13 5 9  -1]
%%
v(2)              % 2. element         -> 5
%%
v([1 3 end])      % elements 1 3 end   -> 13 9 -1
%%
v(1:3)            % elements 1 2 3     -> 13 5 9
%%
v(1:3)=-[1 2 3]   % Update part chosen part of vector.
%                 % Same size or scalar         
%%
v([1 1 end-1 2 1]) % Repetition and changing order allowed.
%%
v([1 3])=NaN       % A form of scalar expansion
                   % NaN  -2.0000   NaN  -1

%% Indexing matrices
clc
clear
format compact
A=[3 33;9 8]
%%
A(1,1)        % --> 3
A(1,2)        % --> 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)
%
%% Some more cases of indexing matrices:
%
A = magic(6)
%%
B = A(3,5)             
%%
C = A([1,2,3],4)        % Sarakkeen 4 alkiot riveilta 1 2 3
%%
D = A(4,[1,1,1])        % [A(4,1) A(4,1) A(4,1)] 
%%
E = A([2,5],[3,1])      % Rivien 2 5 sarakkeet 3 1
%%
F = A(:,4)              % Koko 4. sarake
%%
G = A(4,:)              % Koko 4. rivi
%%
H = A(:)               % Columns of A "sliced" into one long column: 
size(H)                
%%
H'                      % Show transposed to save display space.
A(1:3,[2 3 end-1])=NaN  % Update as before with vectors.
%                        

