%Solutions to the exercises in session 4

%% Exercises with element-wise operations

% Compute S(N) for N = 100
% We can do this by parts:
Vec1 = 1:100; %This will be the base. That 100 represents the N
Vect2 = 1./Vec1; %We now have the vector (1/1, 1/2, ...).
S_100 = sum(Vect2); %End result.
% Or all in one command:
S_100_onecommand = sum(1./(1:100)); % The parenthesis around 1:100 is 
                                    % absolutely necessary!

%Compute G(N) for x = 0.5 and N = 100. We do this in one command:
G_100 = sum( (0.5).^(1:100));


%% Exercises with multidimensional matrices:

%Create a matrix 4x4x3, such that the first layer has 1s in the diagonal,
%the second has 2s, the third has 3s.
A(:,:,1) = eye(4);
A(:,:,2) = 2*A(:,:,1); %or = 2*eye(4,4)
A(:,:,3) = 3*A(:,:,1);

% Create a 6x6x10, such that the first five layers have just 1s, layers
% from 6 to 9 have just 0s, the 10th layer is: (that one)
B(:,:,1:5) = ones(6,6,5);
B(:,:,6:9) = zeros(6,6,4);
B(:,:,10)  = [ones(6,3), zeros(6,3)];


%% Exercises with cells and structures

%Create a vector_cell CellA whose first element is [1], the second [1,2],
%then [1,2,3], etc, until 5. The 6th element is magic(7), the 7th one is
%empty.

%Now, there are two ways. The first one is by initializing first:
CellA = cell(1,7);
CellA{1} = 1; %Note that we index with just 1 number. 
CellA{1,2} = 1:2; %You can also use 2 numbers to index, but it's unnecessary.
CellA{3} = 1:3;
CellA{4} = 1:4;
CellA{5} = 1:5;
CellA{6} = magic(7);
%You don't need to define the 7th. It's already there from the
%initialization and it's empty by default.

%The second possibility is without initializing. So you can just start by
%CellA{1} = 1 and go on until you finish. In this case, you would need to
%make the last element CellA{7} = []; otherwise it won't be created.

%Create a structure called MyStruct with elements: ...
%It doesn't say on the pdf (oops), but you should populate the elements
%NoOfClassmates and CurrentYear with the actual data, so 29 students (or
%so) and 2014.
MyStruct.NoOfClassmate = 29;
MyStruct.CurrentYear = 2014;
MyStruct.MyCell = CellA;
MyStruct.Magia = CellA{6};