0% found this document useful (0 votes)
39 views8 pages

ENG 006 Lecture Notes

This document provides information about various MATLAB functions and concepts including: 1. Logical operators like OR (|), XOR, and NOT (~). It gives examples of using these operators with true/false values and variables. 2. Functions for working with arrays like indexing, transposing, finding minimum/maximum values, and sorting. It describes how to use these functions on 1D and 2D arrays. 3. Plotting functions like plot, title, xlabel, ylabel, ginput, and how to capture points. 4. Other functions like rand, randi, randperm for random number generation. Functions for character operations like lower, upper. 5. Information on for/while

Uploaded by

sheets
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views8 pages

ENG 006 Lecture Notes

This document provides information about various MATLAB functions and concepts including: 1. Logical operators like OR (|), XOR, and NOT (~). It gives examples of using these operators with true/false values and variables. 2. Functions for working with arrays like indexing, transposing, finding minimum/maximum values, and sorting. It describes how to use these functions on 1D and 2D arrays. 3. Plotting functions like plot, title, xlabel, ylabel, ginput, and how to capture points. 4. Other functions like rand, randi, randperm for random number generation. Functions for character operations like lower, upper. 5. Information on for/while

Uploaded by

sheets
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

4/6

- No spaces, special characters, don't start w/ number or under score


Math:
- sind(90) = sin of 90 degrees
- asin(1) = arcsin(1)
- log10(10) = log base 10 (10)
“Doc” log → gives info
“Help” log → info

ANS: D

“Whos” = workspace variable info

Logical Operators:
EX:
Girl = 1 (true)
Boy = 0 (false)
class(Girl) = logical

“~” means not; ~= is not equal to; =~ setting variable as opposite

EX:
Apple = true; pear =~apple
Output: Pear = false

EX:
Girl = 1, boy=~girl
class(Girl) = ‘double’
class(boy) = ‘logical

- or(A,B) // A|B

4/11
Exclusive or operator: checking for a difference in the 2 inputs
- xor(A,B)
False XOR false = false
True XOR false = true

iclicker question:
(x|~x)&0
Output = false
“ && ” it wont perform the second operation
Ex: x=1; (x>5) && (x>10)
“ || “ it won’t perform the other operation, just “|” it will go through both

Scripts:

Functions:
function[finBalance, interest] = BalanceWithInterestBoth(rate, intBalance)

How to call functions:


c= Circumference(10)
X = 10; Circumference(X)

Arrays: # of rows, then # of columns

4/13

rowArray = [x,y,z] or [x y z] %spaces instead is comma

Incrementing arrays = [start:increment:stop]


Ex: [1:100] default increment = 1

linspace(start, stop, # of points)

Column = [x;y;z] % using semicolon

2D array= [rowArray1;rowArray2;Array3]

Logical Arrays = A>3

Transpose: switches column and row arrays


Using ‘ at the end or transpose function

INDEXING:
Linear Indexing = 1 integer row array =
myArray(index#)
1 4
2 5
3 6
EXAMPLE:
Row = [3,4,5,6,7,8]
indexArray = [1,3,5]
Row(indexArray) => [3, 5, 7]

Row-column = 2 integer row array


myArray = ( row, column )

Sub = A([2,3],[2,4])
Row 2,column 2 // row 3, col4

Logic indexing = one logic array


- Will omit the false values when trying to get the original value back
- (M>=130 & M<=150)
011
000
000
M((M>=130 & M<=150))
146
137
max(M((M>=130 & M<=150)))
146
E.g Sum every number over 100 in a matrix = sum(M(M>100))

04/18/23
A = [1,2,3;4,5,6;7,8,9]
A(:,2) % all the rows, second column
A(2,:) = -1 %second row, all the elements -1
A(:,:) = -1 % all columns and all rows // can also do this
A(:) % flattens the array : if the proportions are the same, then it won’t flatten (Video)
A(2,end) % 2nd row last element
A(end-1:end,1:2) the left lower corner 4

“end” = last element

rowA = [1:3]
rowA(end) = 3

04/20/23
Mod(23,5) —> remainder 3
Mod(23,-5) —> remainder -2
Rem(23,5) —> 3

X=[1:12]
Mod(x) —> 012012012

Game: random number generator


Rand(3,4) —> 3x4 array between 0 and 1; does not include end point
Randi(10,3,4) —> a 3x4 array from 0 to 10 integer vals for dice
● Includes max int, but not 0
● Will repeat numbers
Randperm(6,3) —> (N,K) row vector containing K unique integers from 1:N
● does not repeat

Length; 5x2 array —> returns 5


Size: returns [rows,columns]
Numel(A) : returns total elements, 10
Ones(x,y) : x*y array of 1s
Zeroes:
Eye(x,y): diagonal ones

Element operations have a dot before, multiplying arrays needs dot

Plotting:
Plot(arrayx,arrayy)
Title(“..”)
Xlabel(“..” and ylabel(“..”)
Grid on
Ginput(#) %% captures # points; without #, as many as u want
R

4/25
Min and Max
[val1,val2] = min(Array)
Val1 = minimum
Val2 = index
2D array
[minimum,index] = min(array)
Goes through each column and returns the indices separately
957
340
429
Min(array) → 3 , 2, 0 // indices = 2, 3, 2
Sum will add down of the columns if 2D array
sum([1,2]) = 3
sum(x,1) // 1= column ; 2 = rows
- sum(mat2D(:)) %% flattens the array, and then sums all the elements
Prod, mean, mode

Sort
[sortedRow,sortedIndex] = ]sort(2D array)
sortedIndex : index for each column
sortedIndex = 2,3,2
3,2,1
1,1,3
957
340
429
Fliplr = flips left right
Flipud = flip up and down etc.

Sortrow
Doesn’t sort rows,
sortrow(array,#)
Will sort the # column, and the rest of them will be the corresponding row.
Union joins the 2 arrays and removes the repeating

Branching Video notes:


Switch → Case checks if switch and case are equal
Will check until only one case is equal
Switch {x,y} ; can u use curly braces?

If, elseif, else

04/27/2023
Lower(..) = all lower case
Upper(…) - all upper case

For & while loops

05/02/2023
Adding 2D arrays through for loops

Animation
Break
Even if it’s a nested function, the “break” will break out of the nested function AND the while loop
will end

Continue

Will stop the current loop, but will continue for the next iteration

05/09

This one is an empty array, but if the the second line said ‘a’ instead of “a”, then it would
produce a logical index
char(‘Mercury’, ‘Gemini’)
‘Mercury’
‘Gemini ‘ %% automatically adds a space to make the rectangular array the same size

Cell array: {} ; can mix numerical and characters

strcmp case sensitive vs strcmpi(“A”, “a”, n) == 1 true


n = the first n characters

05/11
Struct(fieldname, data)
Function.fieldname = data

Extractedfield = structureName.(stringarray(index))
Orderfields = alphabetical order
When removing or updating the value, u have to reassign
Ex:
Student = Rmfield(student, data)

05/16
Object and classes
The classdef photoFrame name must match the function obj= photoFrame()

Dot notation: frame1.CalcArea


Function syntax: CalcArea(frame1)

05/18
Imagesc() or imshow() to display image
- imshow needs infor from 3 layers

You might also like