70 likes | 86 Views
Learn array operations, functions, data handling, and file imports in MATLAB. Explore exercises and examples on wages and data manipulation techniques.
E N D
Engr 6 Matlab Day 3Array Operations and Functions • Array Operations • Array Ops Exercises • Array Functions • Data: .mat files, • Wages Example • Import from Excel • HtNWt Example • Homework Hints Drop off homework on front table Pick up graded work – alphabetical by last name
Array Operations • +-*/\ with a scalar, affects each array element A*3 • +- with two arrays, sizes match, element by element A+B • */\ with two arrays is matrix multiply (covered later) A*B • .* ./ .\ with two arrays, sizes match, el by el A./B
Array Functions • In pairs, investigate a function on the function examples workbook page using doc. • Formulate 2 to 3 sentences that capture what the function does and the options the programmer has to use the function. • Share with the class.
Data Files: .mat , Load and Save N:/Engineering/Engr6/Widgets.mat • Double click on .mat files to load data from the hard-drive into workspace variables. • Can also use load command or load() function • load Widgets.mat • load(‘Widgets.mat’) • Can save workspace variables to hard-drive. • save Payroll.matEarning Payroll • save(‘Payroll.mat’ , ‘Earnings’, ‘Payroll’)
Importing Data from Excel • >> HtNWt = xlsread(‘Celebrity HtNWt.xlsx’) if the file is in the current directory • OR double click on excel file in current directory opens the Import Wizard adjust data types, variable names, selection range green check to import
Homework Hints • Truncate large data outputs to save paper • Show the first few and last few elements • [1,2,3… 998, 999, 1000] • logspace is tricky • Use doc or help to see how it works • Double check your results • Display formats can be strange • Ans= 1.0e+003* 1 2 3 4 5 … • Must treat data as vectors or matrices • Must use descriptive variable names • Element by Element needs a dot .* • Check sizes to avoid syntax errors
Bonus Challenge • A= rand(4,5); • [x,y]=sort(A); • Output = A([y(:,3)],:) What does each line of code do? What’s the final result of the code?