1D Arrays, 2D Arrays, and Loops

0.0(0)
studied byStudied by 0 people
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/21

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

22 Terms

1
New cards

they let us manipulate:

  • values from a large set

  • every value, individually

  • every value, all at once

what is the use of arrays

2
New cards

place your values inside square brackets [ ] and separate by commas, or whitespace

e.g. [4, 2, 0, 4, 6]

how do you manually create a row vector

3
New cards

place your values inside a square bracket and separate by semi-colon (;) operators

e.g. [4; 2; 0; 4; 6]

how do you manually create a column vector

4
New cards
  • use colon (:) operator

    • e.g. x = 3:9

  • we can also indicate a step-size rather than the default unit-spaced approach

    • e.g. x = 3:2:9

how can we create an array following a linear sequence of increasing/decreasing values

5
New cards
  • y = linspace (start, end, n)

    • n → number of elements

    • we can relate the step-size, i, to the number of elements, n, with the following equation:

      → i = (end - start)/(n - 1)

how do we create an array using the linspace function

6
New cards
  • If we have an end value larger than the start value (or smaller start value the end value if we are decrementing), we may not create any array elements. It could unintentionally happen if we have mixed up the syntax or mistyped something.

  • For example, the following will create an empty array with nothing inside:

    • >> v = 10:1:-2 v = 1×0 empty double row vector

when would we create an empty array when using the colon operator

7
New cards
  • use the parenthesis operation, (), with an index (position) we would like to access

    • example:

    • a (5)

    • a (2:4)

  • we can also assign new values to our array

    • example:

    • a (5) = 4;

    • a (2:4) = [4 4 2]

how can we access our arrays

8
New cards
  • index out of bound errors

  • only use a positive integer index

  • arrays begin at 1

what are the conditions that you must be aware of when accessing arrays

9
New cards
  • enclose the array with [ ]

    • example:

      → D = [4, 5]

      → E = [6, 7]

      → F = [D, E] = [4, 5, 6, 7]

how can we concatenate two existing arrays

10
New cards
  • array addition or subtraction can be performed with scalar values or with other arrays

  • adding or subtracting a scalar will perform the operation on every element

  • in order to add or subtract arrays together, they must have the same dimensions

  • multiplication and division operates the same way, however we must indicate that we want to use element-wise operations, using the .*, ./, or .^

    • e.g. D .* E, D ./ E, D .^ E

how can we perform array arithmetic

11
New cards
  • max (x) finds the maximum element of x

  • min (x) finds the minimum element of x

  • sum (x) finds the sum of all the elements in x

  • mean (x) finds the average value of all elements in x

array functions

12
New cards

norm (x)

what function is used to calculate the length of a vector

13
New cards

dot (x)

what function is used to calculate the dot product of vectors

14
New cards

cross (x)

what is the function used to calculate the cross product of vectors

15
New cards
  • place your values inside square brackets [ ] and separate by commas, or white space to indicate new columns

    • e.g. [1, 2, 3 ,4; 5, 6, 7, 8; 9, 10, 11, 12]

    • use the ; operator to indicate a new row

how are 2D arrays created

16
New cards
  • zeros → creates an array filled with 0 values

  • .’ → transpose operator swaps the rows and columns of an array

  • size → determine the size (of each dimension) of the array

  • length → determine the size of the largest dimension

  • height → return the row length

  • width → return the height length

what functions can be used for 2D array functions

17
New cards

while <expression>

<statements>

end

how do you write a while loop

18
New cards

for <index = initVal:step:endVal>

<statements>

end

how do you write a for loop

19
New cards

for 1:length (A)

<statements>

end

how do we loop over 1D arrays

20
New cards

[num_rows, num_cols] = size(A);

for i = 1:num_rows

for j = 1:num_cols

statements

end

end

how do we loop over 2D arrays

21
New cards
  • To iterate over the rows of array A, where the 𝑖 th-row of array A is:

    • A(i, :): for i = 1:size(A, 1)

      statements

      end

  • To iterate over the columns of array A, where the 𝑗 th-column of array A is A(:, j):

    • for j = 1:size(A, 2)

      statements

      end

how do we loop over entire rows or columns of 2D arrays

22
New cards
  • the break keyword will exterminate the execution of the while or for loop that it is placed in

    • e.g.

      for 1:length (A)

      if <expression>

      <statements>

      break

      end

      statements

      end

  • the continue keyword will skip the execution of the remaining statements in a while or for loop

    • e.g.

      for 1:length (A)

      if <expression>

      <statements>

      continue

      end

      statements

      end

  • the return keyword will skip any remaining statements and terminate the execution of the function or script that it was called from

    • e.g.

      for 1:length (A)

      if <expression>

      <statements>

      return

      end

      statements

      end

how do we use the break, continue, and return keywords