34
Lecture 7 Sept 19, 11 Goals: two-dimensional arrays (continued) • matrix operations • circuit analysis using Matlab image processing – simple examples • Chapter 4 exercises

Lecture 7 Sept 19, 11 Goals:

Embed Size (px)

DESCRIPTION

Lecture 7 Sept 19, 11 Goals: two-dimensional arrays (continued) matrix operations circuit analysis using Matlab image processing – simple examples Chapter 4 exercises. Reshaping Arrays. - PowerPoint PPT Presentation

Citation preview

Page 1: Lecture 7                                                   Sept 19, 11 Goals:

Lecture 7 Sept 19, 11

Goals:

• two-dimensional arrays (continued)

• matrix operations

• circuit analysis using Matlab

• image processing – simple examples

• Chapter 4 exercises

Page 2: Lecture 7                                                   Sept 19, 11 Goals:

Reshaping Arrays

• Arrays are actually stored in column order in Matlab. So internally, a 2 × 3 array is stored as a column vector:

A(1,1)

A(2,1) A(1,2) A(2,2) A(1,3) A(2,3)

• Any n × m array can be reshaped into any p × q array as long as n*m = p*q using the reshape function.

Page 3: Lecture 7                                                   Sept 19, 11 Goals:

Exercise: Write a code segment in Matlab to create a 10 by 10 identity matrix.

Page 4: Lecture 7                                                   Sept 19, 11 Goals:

Matrix operationsMatrix addition, multiplication, inverse, determinant etc.

Page 5: Lecture 7                                                   Sept 19, 11 Goals:

Matrix operationsMatrix addition, multiplication, inverse, determinant, transpose etc.

Page 6: Lecture 7                                                   Sept 19, 11 Goals:

Logical indexing in 2-dim matrices

Page 7: Lecture 7                                                   Sept 19, 11 Goals:
Page 8: Lecture 7                                                   Sept 19, 11 Goals:

Exercise: Solve a linear system of equations: 3x + 5y – 6z= 11 4x – 6y + z = 9 -2x + 3y + 5z = –13

Page 9: Lecture 7                                                   Sept 19, 11 Goals:

Sum, max, min, size etc.

Page 10: Lecture 7                                                   Sept 19, 11 Goals:

Example 2

Page 11: Lecture 7                                                   Sept 19, 11 Goals:
Page 12: Lecture 7                                                   Sept 19, 11 Goals:

4.3. Mixed Data Types

Structure is a variable that can hold a group of data (of different types).

Example:

Page 13: Lecture 7                                                   Sept 19, 11 Goals:

Array of structuresExample:

Page 14: Lecture 7                                                   Sept 19, 11 Goals:

Images as arrays

Page 15: Lecture 7                                                   Sept 19, 11 Goals:

Images as arrays

Numerical representation of array (gray scale image)

Visual representation

Page 16: Lecture 7                                                   Sept 19, 11 Goals:

Reading and opening an image

Page 17: Lecture 7                                                   Sept 19, 11 Goals:

Selecting a subimage

Just like we can copy a part of an array into another array, we can

copy a part of one image and create a new image.

Page 18: Lecture 7                                                   Sept 19, 11 Goals:

Changing some pixel values in an image

Page 19: Lecture 7                                                   Sept 19, 11 Goals:

Saving images in different formats

Image formats:

• jpeg, bmp, png etc.

>> imwrite(I, ‘king.bmp’)

will save I in bmp format.

Page 20: Lecture 7                                                   Sept 19, 11 Goals:

image rotation

Exercise: Write a one-line statement in Matlab that will rotate an image by 180 degrees.

Page 21: Lecture 7                                                   Sept 19, 11 Goals:

image rotation

Exercise: Write a one-line statement in Matlab that will rotate an image by 180 degrees.

>> J = I(size(I,1):-1:1, :, :);

Page 22: Lecture 7                                                   Sept 19, 11 Goals:

Discussions and exercises, Chapter 4

Exercise 4.1

Page 23: Lecture 7                                                   Sept 19, 11 Goals:
Page 24: Lecture 7                                                   Sept 19, 11 Goals:

Exercise 4.2

Write statements to do the following operations on a vector x:

1)Return the odd indexed elements.

Page 25: Lecture 7                                                   Sept 19, 11 Goals:

Exercise 4.2

Write statements to do the following operations on a vector x:

2) Return the first half of x.

Page 26: Lecture 7                                                   Sept 19, 11 Goals:

Exercise 4.2

Write statements to do the following operations on a vector x:

3) Return the vector in the reverse order.

Page 27: Lecture 7                                                   Sept 19, 11 Goals:

Exercise 4.3

Given a vector v, and a vector k of indices, write a one or two statement code in Matlab that removes the elements of v in positions specified by k.

Example:>> v = [1, 3, 5, 7, 11, 9, 19]>> k = [2, 4, 5]>> < your code here>>> vans = 1, 5, 9, 19

Page 28: Lecture 7                                                   Sept 19, 11 Goals:

Exercise 4.3

Given a vector v, and a vector k of indices, write a one or two statement code in Matlab that removes the elements of v in positions specified by k.

Page 29: Lecture 7                                                   Sept 19, 11 Goals:

Exercise 4.4 what does Matlab output for the following commands?

1) 6 ~= 1 : 10

2) (6 ~= 1) : 10

Page 30: Lecture 7                                                   Sept 19, 11 Goals:

Exercise 4.4 what does Matlab output for the following commands?

1) 6 ~= 1 : 10

2) (6 ~= 1) : 10

Page 31: Lecture 7                                                   Sept 19, 11 Goals:

Exercise 4.5. (This is quite tricky, especially without using a loop construct like while or for.)

Write a statement to return the elements of a vector randomly shuffled.

Hint provided is a useful one.

First understand how sort function works.

Page 32: Lecture 7                                                   Sept 19, 11 Goals:

Exercise 4.5. (This is quite tricky, especially without using a loop construct like while or for.)

Write a statement to return the elements of a vector randomly shuffled.

Hint provided is a useful one.

Solution: r = rand(length(x)); [rsort, indices] = sort(x); x(indices)

Page 33: Lecture 7                                                   Sept 19, 11 Goals:

Exercise 4.6.

Write a statement in Matlab to perform the following operations on a vector x:

• return the odd indexed elements of x followed by even indexed elements of x.

Exercise 4.7.

x = reshape(0:2:71, 3, 12). Find the index of 52.

Page 34: Lecture 7                                                   Sept 19, 11 Goals:

SummaryThis chapter introduced you to vectors and arrays. For each collection, you saw how to:

■ Create them by concatenation and a variety of special-purpose functions

■ Access and remove elements, rows, or columns■ Perform mathematical and logical operations on

them■ Apply library functions, including those that

summarize whole columns or rows■ Move arbitrary selected rows and columns from

one array to another■ Reshape arrays■ read an image, modify the array representation,

view, save an image etc.■ use Matlab to solve circuit analysis problems.