Presentation is loading. Please wait.

Presentation is loading. Please wait.

EGR 115 Introduction to Computing for Engineers MATLAB Basics 2: Sub-Arrays Friday 05 Sept 2014 EGR 115 Introduction to Computing for Engineers.

Similar presentations


Presentation on theme: "EGR 115 Introduction to Computing for Engineers MATLAB Basics 2: Sub-Arrays Friday 05 Sept 2014 EGR 115 Introduction to Computing for Engineers."— Presentation transcript:

1 EGR 115 Introduction to Computing for Engineers MATLAB Basics 2: Sub-Arrays Friday 05 Sept 2014 EGR 115 Introduction to Computing for Engineers

2 Lecture Outline Friday 05 Sept 2014 EGR 115 Introduction to Computing for Engineers MATLAB Basics  Sub-Arrays  Displaying data Slide 2 of 10

3 MATLAB basics Sub-arrays: Friday 05 Sept 2014 EGR 115 Introduction to Computing for Engineers 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 [2 8 5] o 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] o i.e., elements a(1,2) and a(1,4) – Since the array 2:2:5 is 2, 4 Answer: a(1), a(4), a(7) ERROR!!! What is the array a(1:3:9)? Slide 3 of 10

4 MATLAB basics Sub-arrays: Friday 05 Sept 2014 EGR 115 Introduction to Computing for Engineers 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!! Slide 4 of 10

5 MATLAB basics Sub-arrays: Friday 05 Sept 2014 EGR 115 Introduction to Computing for Engineers 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 o Machine dependent  ans – stores the result of the last calculation Slide 5 of 10

6 MATLAB basics Sub-arrays: Friday 05 Sept 2014 EGR 115 Introduction to Computing for Engineers Quiz 2.2 on page 43 of textbook a)c(2,:) b)c(:, end) c)c(1:2, 2:end) d)c(6) e)c(4:end) f)c(1:2, 2:4) g)c([1 3], 2) h)c([2 2], [3 3]) Slide 6 of 10

7 MATLAB basics Displaying Data: Display format Friday 05 Sept 2014 EGR 115 Introduction to Computing for Engineers Setting the Default Display Format -> Preferences  Or type in command window >> format … format_examples.m Slide 7 of 10

8 MATLAB basics Displaying Data: The “disp” and “fprint” functions Friday 05 Sept 2014 EGR 115 Introduction to Computing for Engineers Using the “disp” function >> disp(pi) >> disp(['The value of pi = ', num2str(pi)]) The “fprintf” function (borrowed from the C-language)fprintf  Typical use: fprintf(format_spec, variable_list) 3.1416 Format (conversion char) Description %dDecimal notation %eExponential notation %fFixed-point notation %gMore compact notation (%f or %e) \nNew line Slide 8 of 10 Concatenate the strings

9 MATLAB basics Displaying Data: The “fprint” function Friday 05 Sept 2014 EGR 115 Introduction to Computing for Engineers 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 o Field width is the min number of total digits to be printed o 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!! Slide 9 of 10

10 Next Lecture Friday 05 Sept 2014 EGR 115 Introduction to Computing for Engineers Data files, Array Operations, and Some of the built in functions Slide 10 of 10


Download ppt "EGR 115 Introduction to Computing for Engineers MATLAB Basics 2: Sub-Arrays Friday 05 Sept 2014 EGR 115 Introduction to Computing for Engineers."

Similar presentations


Ads by Google