20 - Subarrays and Vectorization - Full

20 - Subarrays and Vectorization - Full - Engineering101...

Info icon This preview shows pages 1–16. Sign up to view the full content.

View Full Document Right Arrow Icon
Click to edit Master subtitle style Engineering 101 Subarrays and Vectorization
Image of page 1

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

View Full Document Right Arrow Icon
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.
Image of page 2
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]
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
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
Image of page 5

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

View Full Document Right Arrow Icon
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
Image of page 6
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
Image of page 7

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

View Full Document Right Arrow Icon
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
Image of page 8
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 0 3 0 5 0 7 0 9 0 11 0
Image of page 9

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

View Full Document Right Arrow Icon
Exercise Which DOES NOT create the following matrix? -1 -1 0 0 0 0 0 -1 -1 0 0 0 0 0 0 0 3 3 3 0 0 0 0 3 3 3 0 0 0 0 0 0 0 -1 -1 0 0 0 0 0 -1 -1 1 2 3
Image of page 10
Exercise Which DOES NOT create the following matrix? -1 -1 0 0 0 0 0 -1 -1 0 0 0 0 0 0 0 3 3 3 0 0 0 0 3 3 3 0 0 0 0 0 0 0 -1 -1 0 0 0 0 0 -1 -1 1 2 3
Image of page 11

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

View Full Document Right Arrow Icon
Which function swaps the elements in the even  numbered positions of a vector with those in the odd  numbered positions? 1 4 3 2
Image of page 12
Which function swaps the elements in the even  numbered positions of a vector with those in the odd  numbered positions? 1 4 3 2
Image of page 13

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

View Full Document Right Arrow Icon
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. n These expressions are almost always faster  than  for  or  while  loops  
Image of page 14
Vectorizing Expressions n When we create a matrix by using a logical  operator that array is a  logical array x = [ 1 7 10; 9 4 2 ]; y = x > 5; n We can also make any array logical by using  the  logical  function z = logical(x);
Image of page 15

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

View Full Document Right Arrow Icon
Image of page 16
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern