% CS 221 Problem Set 2,  Part 3

%%
% Create a row vector V0 with 10 random elements.
V0 = rand(1, 10)


%%
% Set V1 so its elements are exactly twice those of V0.
% First way:
V1 = 2*V0

% Or use a loop:
V1 = zeros(1, length(V0))
for index = 1:length(V0)
    V1(index) = 2*V0(index)
end

%%
% Set V2 so its elements are five times those of V1.
% First way:
V2 = 5*V1

% Or use a loop:
V2 = zeros(1, length(V1))
for index = 1:length(V1)
    V2(index) = 5*V1(index)
end

%%
% Define L0 to be an array equal to the first 4 elements of V0
% First way:
L0 = V0(1:4)

% Or use a loop:
L0 = zeros(1, 4)
for index = 1:4
    L0(index) = V0(index)
end


%%

% Assign the fifth and sixth elements of V1 to L2. 
L2 = V1(5:6)

% Or use a loop:
L2 = zeros(1, 2)
for index = 5:6
    L2(index-4) = V1(index)
end
    
%%

% Create a 3 x 10 matrix A whose first row is equal to V0, second row is equal to V1 and third row is equal to V2. 
% A = [V0; V1; V2]

% Another way would be to insert each element using a loop
A = zeros(3, 10)
for index = 1:length(V0)
    A(1, index) = V0(index)
    A(2, index) = V1(index)
    A(3, index) = V2(index)
end

%%

% Swap the first and third rows of A. (Use any method you can think of.)
% NOTE: We show three different methods below. Your script should have at
% most ONE of these.
% Method 1
for i = 1:length(V0)
    temp = A(1, i)
    A(1, i) = A(3, i)
    A(3, i) = temp
end

% Method 2:  (Note: this changes back to the original array
A = [V2; V1; V0]

% Method 3:  (Note: this is now the correct answer)

A = [A(3,:); A(2,:); A(1,:)]
