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

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

Embed Size (px)

Citation preview

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

EGR 115 Introduction to Computing for Engineers

MATLAB Basics 2: Sub-Arrays

Friday 05 Sept 2014 EGR 115 Introduction to Computing for Engineers

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

Lecture Outline

Friday 05 Sept 2014 EGR 115 Introduction to Computing for Engineers

• MATLAB Basics Sub-Arrays Displaying data

Slide 2 of 10

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

MATLAB basicsSub-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

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

MATLAB basicsSub-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);

3 4 5

8 9 10

13 14 15

:,3 : 5M

5

10

15

:,endM

An error will occur if the LHS & RHS sizes are different!!Slide 4 of 10

Bruder, Stephen
Page 5: EGR 115 Introduction to Computing for Engineers MATLAB Basics 2: Sub-Arrays Friday 05 Sept 2014 EGR 115 Introduction to Computing for Engineers

MATLAB basicsSub-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 MATLABo Machine dependent

ans – stores the result of the last calculation

Slide 5 of 10

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

MATLAB basicsSub-arrays:

Friday 05 Sept 2014 EGR 115 Introduction to Computing for Engineers

• Quiz 2.2 on page 43 of textbooka) 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])

1 2 3 4

5 6 7 8

9 10 11 12

c

Slide 6 of 10

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

MATLAB basicsDisplaying 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

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

MATLAB basicsDisplaying 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) 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

Slide 8 of 10

Concatenate the strings

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

MATLAB basicsDisplaying 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 precisiono Field width is the min number of total digits to be printedo Precision is the number of decimal places

>> fprintf('Compare: pi = %5.0f pi = %5.3f pi %5.8f \n', pi, pi, pi);

Format (conversion char) Description

%d Decimal notation

%e Exponential notation

%f Fixed-point notation

%g More compact notation (%f or %e)

\n New line

fprintf in MATLAB is NOT exactly the same as the original C-function!!

Slide 9 of 10

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

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