% Class 2 Script
% Coded by NFR on 1.17.19
% This records how to use bits of MATLAB
%
% Different types of variables
% Scalar - is a number,
A = 12;
B = 13.6;
C = A+B;
% Vector - (row vs. column)
Row_vec = [1 4 6 8 10];
Col_vec = [2; 4; 5; 9; 12];
% Matrix
M = [1 2 3; 4 5 6; 7 8 9];
% Accessing a number in an array
E1 = M(2,3); % specify row, then column
% You can use the transpose function to change a row vector to column
% vector
RowVec2 = Col_vec';
% Strings = text
Str1 = 'I love matlab';
Str2 = '...Really.';
str3 = [Str1,Str2];
% Loading data into matlab
D1 = xlsread('CornDataHeaders.xlsx');
% To inspect the first row of the data file
D1(1,:) % Where : means all columns)
% Yes, it looks like we got the data
plot(D1(:,1),D1(:,11),'o') % More on plotting, next Tues
% What about loading text data?
[num,txt,raw] = xlsread('CornDataHeaders.xlsx');
% Now we can grab the header names
Header = txt(1,:)
% To access one element of the header cell array
Header{1}
% With your group or individually, open the Olympic data
[O_data,O_txt,~] = xlsread('SummerOlympicsData.xlsx');
% If you have a comma separated value use 'csvread'
% Any other delimited file - 'dlmread'
%
% Mathematical Operators
A = 12;
B = 25;
C = A+B;
C = A-B;
C = A/B; % NOTE: do not use \ as division, this is for linear algebra (mega cool)
D = (A+B)/C;
C = A^B;
C = A^(1/2);
C = sqrt(A); %Same as above
% A note on element by element operator
% This is important when doing math with array
E1 = M^2
E2 = M.^2 % <-- This '.' denotes that we are doing the operation one element at a time, NOT matrix math
% Prepare olympic data for analysis!
O_header = O_txt(1,:);
Vec_nations = O_txt(2:end,1); % : is used here to show range of numbers
Vec_numgames = O_data(:,1); % : here has no range, so we assume all rows
G_vec = O_data(:,2);
S_vec = O_data(:,3);
B_vec = O_data(:,4);
% Show use of built in function with this data
% What is the total number of gold medals awarded ever?
Gold_Total = sum(G_vec)
% Max number of gold medals
Gold_max = max(G_vec)
% What country did this?
[Gold_max,ind] = max(G_vec);
% report which country is at that index
Vec_nations(ind)
% Examoles of other built in functions
MinG = min(G_vec)
AvgG = mean(G_vec)
VecSort = sort(G_vec) % Again, with this you can have a second output, that tracks the index
[VecSort,ind2] = sort(G_vec,'descend')
NationSort = Vec_nations(ind2);
NationSort(1:10)
% Rounding numbers
A = 3.752;
B = round(A)
B = floor(A)
B = ceil(A)
% Logarithms (more built in functions)
A = 100;
B = log(A) %Natural log (ln)
B = log10(A) % Base 10 log
% Creating Vectors - use this when you want to make a reg spaced array
Vec1 = linspace(1,100,50) % Created vector 1 to 100 with 50 total points
Vec2 = (1:0.5:100) % This creates array 1 to 100, but steps by 0.5 increments
Vec3 = logspace(1,100,10)
% Also create vector/matrices of ones or zeros
Mat1 = ones(3,3)
Mat2 = zeros(3,3)
% Random numbers
A = rand %Creates random number from 0 to 1;
% How you can simulate a 6 sided dice
input('Guess a number from the dice..')
Roll = ceil(rand()*6)