% Class 2 - Examples % Coded by Nigel F Reuel on 8.29.19 % % Types of Variables % % Numbers (scalar) A1 = 12; B2 = 13.4768; % Text (string) A = 'Hello world!' B = 'My name is...' % Combine strings by concantenation C = [A B] % Example of a column vector ColVec = [1; 2; 3; 4] % Example of a row vector RowVec = [1 2 3 4] % Example of a matrix (3x3) Matrix = [1 2 3; 4 5 6; 7 8 9] % How to access elements of a matrix % (m,n) notation where m is the row, and n is the column PickNum = Matrix(1,3) Pick2Num = Matrix(2,1:2) % The : means 'to' % Concantanate Vectors and Matrices, but they need to have the right % dimensions ColVec2 = [10; 11; 12]; Combined = [Matrix ColVec2] % You can transpose a vector (change from row to column) ColVec2_t = ColVec2' % % Uploading Data from a file (usually excel) % Using xlsread (NOTE: might disappear in next year) D = xlsread('IowaStateFair.xlsx') % Default, only numbers, no text [num,txt,~] = xlsread('IowaStateFair.xlsx') % Now we have the text info in a cell array % To access info in the cell array use {} Row4Text = txt{4,1} % Answer: what is the attendance in 2019 on Day 5 Answer = num(6,2) % The NEW way to open excel (or other data files) % WARNING: if you don't have v 2019 it won't work NumData = readmatrix('IowaStateFair.xlsx') TextData = readcell('IowaStateFair.xlsx') % % Next: Mathmatical Operators % Common: + - * / <-- Divide is this slash % This slash \ is for matrix math (super powerful) % Exponents (^) W = 2^5 % Scientific notation 'e' B = 1.35e4 % Element wise notation (this is used w/ matrices and vectors) % . by a *,/, or ^ means you use this operator % on each of the elements M2 = Matrix./2 % This divides each element by 2 % Common built in math functions % Log base 10 vs natural log R = log(10) % this is natural log R = log10(10) % Rounding numbers n1 = 2.356 n2 = round(n1) % standard rouding rules n3 = ceil(n1) n4 = 2.789 n5 = floor(n4) % Other command to use on vectors (lists of numnbers) % sum, min, max, mean, sort % Example for total attendance in 2019 % num is the variable with the attendance data Total_2019 = sum(num(2:end,2)) % except the last date Total2_2019 = sum(num(2:end-1,2)) % Average daily attendance in 2018 Avg_2018 = mean(num(2:end,1)) % % Creating a vector of numbers that are % equally spaced (plotting) Tvec = linspace(1,100,300) % This is time values 1 to 100s, with 300 steps % Another way is to use (#:#:#) notation Tvec2 = (1:0.5:100) % Remember the middle number is the step size % Create vectors of a single number % I need a vector of 100 zeros Zvec = zeros(100,1) OneVec = ones(100,1) % Random Numbers (many generators) % Two most common UniformRand = rand(1) % This is uniform random from 0 to 1 IntegerRand = randi(6) % Built in variables to Matlab pi i % You can request information from your user V3 = input('How many scoops of ice cream did you get?'); % You can also display text: disp('One more scoop would be:') ans2 = V3 + 1 % Another way to do output of text and numbers % (this can look nicer) is to use fprintf