% HW_02: due July 13th, 2016
% EXCEL EA-1
clear % Delete all variables
clc % Clear the Command Window
close all % Close any open figures
format compact % Remove blank lines in Command Window output
% Follow the instructions in each cell. Lines marked "%..." indicate where
% you should insert code.
% You must add a comment to EVERY LINE OF CODE. This comment can be either
% on the line before the code, or on the same line at the end.
%% (a) Dot product
% Write a short program which calculates the vector dot product between two
% vectors a and b. Your program should work for any length of vectors, as
% long as a and b have the same length. (Hint: check out the "length"
% command.
% The dot product for two vectors a=[a1, a2, a3, ...], b=[b1, b2, b3, ...]
% is defined as (a1*b1 + a2*b2 + a3*b3 + ...)
len = ceil(16*rand); % create a random integer between 1 and 16
a = rand(1, len); % create a vector with random elements of length "len"
b = rand(1, len);
%... Put your code here. Calculate a dot b without using the "dot" command
% Check your answer using the "dot" command
real_answer = dot(a, b)
%% (b) Matrix multiplication
% Write a short program which multiplies two matrices, A and B. First,
% check that the sizes are compatible. Your program should work for any
% random matrices, regardless of size (provided that the inner dimensions
% match).
M = ceil(6*rand); % number of rows in A
N = ceil(6*rand); % number of cols in A AND rows in B
L = ceil(6*rand); % number of cols in B
A = rand(M, N); % Create matrices with random elements
B = rand(N, L);
%if ... first check that the dimensions are compatible
%C = zeros(... next, create a matrix of zeros to store the result
%Calculate the elements of C one by one
%...
%end
% Check your answer using the * command
real_answer = A * B
%% (c) for loops practice
% Write a program which adds up all the elements in the "upper-right"
% trangle of a matrix. For example, for the matrix
% A = [ 1 2 3 4;
% 5 6 7 8;
% 9 10 11 12;
% 13 14 15 16]
% The upper-triangular elements are:
% UT = [ 1 2 3 4;
% 6 7 8;
% 11 12;
% 16]
% and the sum is 1+2+3+4+6+7+8+11+12+16 = 70
% Hint: use nested for loops to loop over the rows and columns
clear; clc; close all;
N = ceil(6*rand); % pick a random size for A
A = rand(N, N); % create an NxN matrix with random elements
%for r = ... loop over all rows
% for c = ... which columns to loop over?
%...
% end
%end
% Check your answer:
real_answer = sum(sum(triu(A)))
%% (d) While loops
% Re-use your code from the rock-paper scissors game you wrote, but this
% time alter your program so that you keep playing until you win 3 times.
% You may want to use the following code somewhere in your program:
% Choose a random number between 1 and 3 to represent the computer's hand
% comp_hand = ceil(3*rand());
% Let the human choose a value
% human_hand = input('Choose your hand: (1) paper, (2) scissors, (3) rock: ');
% Remember determine who wins and print a statement such as:
% disp('You won!')
% disp('You tied.')
% disp('You lost...')
%...