matlab Flashcards
(39 cards)
What are variables?
Variables are memory stored within the hardware of computers and are used to store data that can be either strings or numbers
What are the rules for creating variable names in MATLAB?
- Variables must start with a non-numerical character
- Variable names can include lowercase, uppercase, numbers and underscores
- Variable names cannot exceed a certain length (differs for different computer systems)
What does the assignment operator do in MATLAB?
Stores a variable under a variable name by changing the memory in the computer (assigns a new value to the location in the computer)
What do operators do?
Operators manipulate data by taking data values as input and either:
1. producing some output value
2. changing the memory of the machine
What functions can be used to output messages?
- disp – e.g. disp(“Hello world”);
-to display multiple things, disp([variable1, variable2]); - fprintf – e.g. fprintf(“Hello world”);
What is the syntax for taking string inputs from the user?
variableName = input(“string”, “s”);
What is the process of programming?
To automate a computational process by transforming data in a computer’s memory
What are examples of operations?
- A computer’s memory
- Multiplication
- Addition
- Assignment
What parts make up a variable?
- Name
- Data
What is computer memory?
A long sequence of locations where data can be stored
What are vectors?
1-dimensional data structure for grouping multiple pieces of data (an nx1 matrix).
-Use square brackets to assign elements
e.g vector = [1 2 3]
What are columns and rows?
Columns go across, rows go down
When to use a for-loop instead of a while loop?
For loops are used when the number of repetitions for code is known, e.g. wanting to loop to roll a dice 6 times.
While loops are used when the number of loops required is unknown, e.g. rolling a dice until an even number is rolled.
What is the for-loop syntax?
for i = 1:n – from 1 to n times
for i = 1:0.5:n – from 1 to n in 0.5 increments
for i = n:-1:1 – from n to one in increments of 1
How to access elements in a vector?
Use roundbrackets – MATLAB element numbering starts at 1
e.g. vector = [1 2 3]
vector(1) = 1
What is concatenation?
The process of joining 2 or more parts (vectors) together
How are vectors in MATLAB stored?
Vectors are automatically flattened when containing nested vectors
e.g. [ [1] [2] ] –> [1 2]
What is appending?
Adding data to the end of existing data
What is prepending?
Adding data before a piece of existing data
What is the syntax for using the zeros command?
variableName = zeros(rows, columns)
What is the difference between using “string” and ‘string’?
“String” stores the entire word as an element, whereas ‘string’ stores each individual letter as an element.
e.g string = [“String” “Low” “Me”]
string(3) = “Me”
string = [‘String’ ‘Low’ ‘Me]
string(3) = ‘r’
What are some built in string functions in MATLAB?
- upper(a) - converts a to uppercase
- lower(a) - converts a to lowercase
- findstr(date, ‘/’) - searches for ‘/’ in date
- strcmp(test, ‘apple’) - compares test to apple
strtrim(‘North Terrace’) - removes leading and trailing spaces from ‘North Terrace’
How to make 2D arrays?
Use ‘;’ between elements when a new row is being made – each row has to have same number of elements
How would we iterate over each element in a 2D array?
By using a nested loop - the outer loop iterates over the rows while the inner loop iterates over the columns