EGR 115 Introduction to Computing for Engineers MATLAB Basics 2: Sub-Arrays Lecture 5 EGR 115 Introduction to Computing for Engineers
EGR 115 Introduction to Computing for Engineers Lecture Outline MATLAB Basics Sub-Arrays Displaying data Lecture 5 EGR 115 Introduction to Computing for Engineers
MATLAB basics Sub-arrays: A sub-array is a subset of a larger array >> a = [ 1 3 8 5 9 4]; The sub-array a(2:4) is the array [3 8 5] i.e., elements a(1,2), a(1,3), and a(1,4) Or elements a(2), a(3), and a(4) The sub-array a(2:2:5) is the array [3 5] i.e., elements a(1,2) and a(1,4) Since the array 2:2:5 is 2, 4 2:4 is the same as 2:1:4 What is the array a(1:3:9)? Answer: a(1), a(4), a(7) ERROR!!! Lecture 5 EGR 115 Introduction to Computing for Engineers
MATLAB basics Sub-arrays: Considering 2D arrays >> M = [ 1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15]; The sub-array M(:,3:5) corresponds to all rows and cols 3, 4, 5 We can also index from the “end” >> M(:,end) refers to the last column We can reassign a sub-set of the array >> M(1:2, [3 4]) = zeros(2,2); An error will occur if the LHS & RHS sizes are different!! Lecture 5 EGR 115 Introduction to Computing for Engineers
MATLAB basics Sub-arrays: Some special predefined values pi 3.14159… to 15 significant digits 1i The square root of -1 i.e., an imaginary number Inf Result of dividing by zero, i.e. infinity NaN Not-a-Number (undefined result) clock date & time = [year, month, date, hr, min, sec.sec] eps smallest possible number in MATLAB Machine dependent ans stores the result of the last calculation Lecture 5 EGR 115 Introduction to Computing for Engineers
MATLAB basics Sub-arrays: Quiz 2.2 on page 44 of textbook c(2,:) c(:, end) c(1:2, 2:end) c(6) c(4:end) c(1:2, 2:4) c([1 3], 2) c([2 2], [3 3]) What is the size of each? Lecture 5 EGR 115 Introduction to Computing for Engineers
MATLAB basics Displaying Data: Display format Setting the Default Display Format -> Preferences Or type in command window >> format … format_examples.m Lecture 5 EGR 115 Introduction to Computing for Engineers
MATLAB basics Displaying Data: The “disp” and “fprint” functions Using the “disp” function >> disp(pi) >> disp(['The value of pi = ', num2str(pi)]) The “fprintf” function (borrowed from the C-language) Typical use: fprintf(format_spec, variable_list) 3.1416 Format (conversion char) Description %d Decimal notation %e Exponential notation %f Fixed-point notation %g More compact notation (%f or %e) \n New line Lecture 5 EGR 115 Introduction to Computing for Engineers
MATLAB basics Displaying Data: The “fprint” function fprintf examples: Compare effects of the various conversion chars >> fprintf('Compare: pi = %d pi = %e pi = %f pi = %g\n', pi, pi, pi, pi); We can also control the field width and precision Field width is the min number of characters to be printed Precision is the number of decimal places >> fprintf('Compare: pi = %5.0f pi = %5.3f pi %5.8f \n', pi, pi, pi); fprintf in MATLAB is NOT exactly the same as the original C-function!! Lecture 5 EGR 115 Introduction to Computing for Engineers
EGR 115 Introduction to Computing for Engineers Next Lecture Data files, Array Operations, and Some of the built in functions Lecture 5 EGR 115 Introduction to Computing for Engineers