20 - Subarrays and Vectorization - Full

20 - Subarrays and Vectorization - Full - Click to edit...

Info iconThis preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Click to edit Master subtitle style Engineering 101 Subarrays and Vectorization Quote of the Day- Mahatma Gandhi To conceal ignorance is to increase it. An honest confession of it, however, gives ground for the hope that it will diminish some day or the other. Using Subarrays n You can use pieces of arrays just as you would arrays. n For example if you have an array x = [ 1 7 4 –1 13 ] n x(3) is 4 n x([1 3 5]) is [1 4 13] Using Subarrays n In addition you can use : notation to select elements. end denotes the last element. a = [ 1 5 8 –10 3 21 9 ] b = a(1:2:end) n A colon on its own selects all values x = [ 7 8 9 ; 1 2 3; -1 -2 -3 ] y = x( :, 1:2 ) b = [ 1 8 3 9 ] y = 7 8 1 2-1 -2 Using Subarrays n You can also assign to subarrays arr = [ 1 2 3 4; 5 6 7 8; 9 10 11 12 ] 1 2 3 4 5 6 7 8 9 10 11 12 Using Subarrays n You can also assign to subarrays arr = [ 1 2 3 4; 5 6 7 8; 9 10 11 12 ] arr([1 3],1:2) = [ -4 -3; -2 -1 ] 1 2 3 4 5 6 7 8 9 10 11 12 Using Subarrays n You can also assign to subarrays arr = [ 1 2 3 4; 5 6 7 8; 9 10 11 12 ] arr([1 3],1:2) = [ -4 -3; -2 -1 ]-4 -3 3 4 5 6 7 8-2 -1 11 12 Using Subarrays n You can also assign to subarrays arr = [ 1 2 3 4; 5 6 7 8; 9 10 11 12 ] arr( : , [2, 4] ) = 0 1 2 3 4 5 6 7 8 9 10 11 12 Using Subarrays n You can also assign to subarrays arr = [ 1 2 3 4; 5 6 7 8; 9 10 11 12 ] arr( : , [2, 4] ) = 0 1 3 5 7 9 11 Exercise Which DOES NOT create the following matrix?-1-1-1-1 3 3 3 3 3 3-1-1-1-1 1 2 3 Exercise Which DOES NOT create the following matrix?-1-1-1-1 3 3 3 3 3 3-1-1-1-1 1 2 3 Which function swaps the elements in the even numbered positions of a vector with those in the odd numbered positions? 1 4 3 2 Which function swaps the elements in the even numbered positions of a vector with those in the odd numbered positions? 1 4 3 2 Vectorizing n It is possible to use for-loops to perform mathematical operations on arrays. n But taking advantage of MATLAB’s natural ability to work with arrays and vectors greatly speeds up code....
View Full Document

This note was uploaded on 04/07/2010 for the course ENGR engr 101 taught by Professor Rinenberg during the Spring '10 term at University of Michigan.

Page1 / 40

20 - Subarrays and Vectorization - Full - Click to edit...

This preview shows document pages 1 - 15. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online