Sei sulla pagina 1di 4

== : used when you want to see IF it is equal to something

False: 0
True: 1
Input: parenthesis + need comma
Output: brackets + doesnt need comma

Vectors:

Creating a vector:
V1 = [_ _ _ _ _ _]
V1 = 1:3:12

numbers 1-12, in steps of 3

V1 = linspace (first #, last#, # of #s)


V1 = ones or zeroes(row,column)
V1 = rand(row, column) random numbers are between 0 and 1 and are uniformly
distributed
V1 = randn(row, column) rand w/mean of 0 and stdev of 1: normal distribution

Indexing a vector:

V1(8) gives you the 8th number in a vector


Ndx = 4:-1:1 4 3 2 1
V1(ndx) gives you 4th #, then third, .

Shortening Vectors:
V1 = [1,2,3,4,5,6]
V1 = V1([1:2 4:end]) keep elements 1-2 and 4-end. i.e. delete third term
Logical operations on vectors:

Finding which values in D are bigger than B


isBigger = D > B gives you something like [1 1 0 1 0 1] (true true false true.)
ndx = find(isBigger)
bigs = D(ndx) gives you in a vector, the values of D which are bigger than
B
alsoBigs = D(isBigger) basically indexing with a logical vector
( isBigger). Returns the
same result. Keep in mind that if the logical vector is longer than the D
vector, then
every logical value past the length of D will result in false.

Finding values of D which are even


Evens = mod(D,2) == 0 gives logical result on which values are even
evenVals = D(evens) gives actual values of even numbers in Vector D

What values are in even positions in D


Ndx = 2:2:length(D)
Evenpos = D(ndx)
BETTER WAY: evenpos = D(2:2:end)

Fine / Halve the biggest value in D


[value where] = max(D) gives the value, and position where the maximum value
in D occurs
D(where) = D(where) . /2
Note: Make three values in D the max value D([end+1, end+3]) = max(D)
Note: Find all maxes of D if there are multiple
Where = D == max(D)
D(where) = D(where) ./ 2

Finding numbers in vec which are bigger than 3


vec = [2 3 4 5]
vec(vec>3) gives values of vec which are bigger than 3
sorting vectors in increasing order
sort(vec)
Arrays
Array = [3,7;4,5;13,2]; semicolon moves to next row
Finding the number of rows/columns in an array
Size(array) gives # of rows then columns
Size(array, 1) gives number of rows
Size(array,2) gives number of columns
Size(array, 3) gives number of inputs in third dimentionetc
Indexing Arrays
Arr(2,3) row 2 column 3
: indicates all columns or rows
Arr([1 3], [2 4]) values where rows 1 and three intersect with columns 2 and 4 (4
values)
When deleting, must delete full row/column
Linearize
Arr(:)
Size:
[r c] = size(arr) gives rows and columns
Finding the top half of an array
Top half = arr(1:end/2,:)
Bottom half = arr(end/2+1:end , : )
Transposing array:

Arr

Summing arrays:
Sum(arr) sum of each columns
Sum(sum(arr)) sum of all numbers
Sum(arr) = sum(arr,2) sum of rows
Vertically Concanate
Vertcat(arr1, arr2)
Reshape
Reshape(matrix,#ofrowsthatyouwant, #ofcolumnsthatyouwant)

Potrebbero piacerti anche