{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Arrays-by Toshant - Arrays 1 Arrays vs Vectors a All...

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

View Full Document Right Arrow Icon
Arrays 1. Arrays vs. Vectors a. All vectors are pretty much just one row/column arrays. 2. Creating Arrays and Array Properties a. Arrays must be rectangular. They cannot be jagged. a.i. Meaning that if row 1 has two values, row 2 must also have two values. a.ii. If you try to make a jagged array, Matlab will either error or fill in the empty spot with zeros, depending on the command. b. Creating Arrays b.i. Direct Creation b.i.1. Arr = [1 2; 3 4; 5 6] -> Creates a vector of three rows and two columns. b.i.1.a. The semicolon creates vertical concatenation. So it creates the vectors [1 2], [3 4], and [5 6] and then places them on top of each other. b.i.1.a.i. Notice, if you want to vertically concatenate items, they must have the same number of rows. b.ii. Functions b.ii.1. Colon operator (:) and linspace cannot create an array. They can only create vectors. b.ii.1.a. You might have to get crafty to place these values in an array. b.ii.2. ones, zeros, and rand Functions b.ii.2.a. All of these functions take two inputs ( row , col ). b.ii.2.b. So, row refers to the number of rows, and col refers to the number of columns. b.ii.2.b.i. Remember how when we created a vector, we would do (1, 4) to create a vector of 4 values. b.ii.2.c. x = ones(7, 4) b.ii.2.c.i. The code above creates an array of ones. The array has 7 rows, and 4 columns. c. Accessing and Indexing Arrays c.i. Note : Most rules of Vector indexing rules apply to arrays. (Can't index out of bounds, but you can assign out of bounds, etc. Read up on vector notes for full rules) c.ii. Basics of Array Indexing c.ii.1. Arrays are indexed using the row and column references. c.ii.2. Assume: arr = [1 2; 3 4; 5 6]; c.ii.3. x = arr(3, 2) c.ii.3.a.The code above gets the value that's stored in the 3rd row and 2nd column. c.ii.3.b. x -> 6 c.ii.4. x = arr(7, 2)
Image of page 1

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

View Full Document Right Arrow Icon
c.ii.4.a.So we're going to the 7th row of arr, and the 2nd column. But the 7th row does not exist. c.ii.4.b. Therefore, Matlab will error. d. Shorthand Operators d.i.1. end operator d.i.1.a. In vectors, indexing with end references the end position of the vector. d.i.1.b. In arrays, the end position references the last position of the array in the reference position. d.i.1.b.i. x = arr(end, 1) d.i.1.b.i.1. The code above goes to the end row position and 1st column. d.i.1.b.ii. x = arr(1, end) d.i.1.b.ii.1. The code above goes to the end column, aka the last column, and the 1st rwos. d.i.1.b.iii. x = arr(end, end) d.i.1.b.iii.1. The code above goes to the end column and end row, so the last column and last row. d.i.2. Colon Operator d.i.2.a. In vectors, we used the colon operator to get more than one value. That same logic can be applied to the row and column assignments of an array. d.i.2.b. In arrays, you can use the colon operator in reference to rows and columns. d.i.2.c. Remember that simply putting the colon (:) does the same thing as 1:end.
Image of page 2
Image of page 3
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