1/21
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
they let us manipulate:
values from a large set
every value, individually
every value, all at once
what is the use of arrays
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
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
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
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
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
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
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
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
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
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
norm (x)
what function is used to calculate the length of a vector
dot (x)
what function is used to calculate the dot product of vectors
cross (x)
what is the function used to calculate the cross product of vectors
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
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
while <expression>
<statements>
end
how do you write a while loop
for <index = initVal:step:endVal>
<statements>
end
how do you write a for loop
for 1:length (A)
<statements>
end
how do we loop over 1D arrays
[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
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
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