Matlab Flashcards
• MATLAB variable and file name conventions
cant start with a number
cant be the name of a fucntion
cant exceed namelengthmax
or be a keyword
when to use ()
Use parentheses to specify precedence of operations, enclose function input arguments, and index into an array.
also indexing
when to use []
Array construction
Array concatenation
Empty matrix and array element deletion
Multiple output argument assignment
when to use {}
Cell array assignment and contents
Difference between script & function
scripts can be run on their own but functions need to be called
input
x = input(prompt)
output
fprintf(‘Happy Birthday\n’);
fprintf(‘My name is %s\n’, prof_name);
disp(X)
syntax
missing semicolon etc
global workspace
avalable to everything
local workspace
only avalable to the function that it is called in
• What is considered “good practice”?
formatting and commenting
2d plots
plot(X,Y)
indexing arrays
do it 2d like
matrix(Row,Collum)
matrix with integer referencing
matrix(num) num is the number of the array
Array resizing and reshaping
ASK
loading files matlab
load(filename)
Element-wise operators
.* .^ ./
Transposing arrays
array’
Clc
Clear command window
Close
Clear system memory
find
k = find(X) returns a vector containing the linear indices of each nonzero element in array X.
ans
funcitons as the most recent answer
• Iskeyword
Tests if iskeyword(word) is a
• zeros
creates an array of zeroes (row,collum)
ones
creates an array of ones (row,collum)
rand
uniformly distributed random numbers
randn
Normally distributed random numbers
randi
Uniformly distributed pseudorandom integers
X = randi(imax) returns a pseudorandom scalar integer between 1 and imax.
linspace
y = linspace(x1,x2) returns a row vector of 100 evenly spaced points between x1 and x2.
logspace
y = logspace(a,b,n) generates n points between decades 10^a and 10^b.
length
n = length(X) returns the size of the longest dimension of X. If X is a vector, this is the same as its length.
size
d = size(X) returns the sizes of each dimension of array X in a vector d with ndims(X) elements.
numel
n = numel(A) returns the the number of elements, n, in array A.
sort
B = sort(A) sorts the elements of A in ascending order.
If A is a vector, then sort(A) sorts the vector elements.
If A is a matrix, then sort(A) treats the columns of A as vectors and sorts each column.
’
transposes teh array
end
terminates the block of code
round
= round(X) rounds each element of X to the nearest integer. In the case of a tie,
xor
C = xor(A,B) performs a logical exclusive-OR of arrays A and B and returns an array containing elements set to either logical 1 (true) or logical 0 (false)
max
M = max(A) returns the maximum elements of an array.
mean
M = mean(A) returns the mean of the elements of A
median
M = median(A) returns the median value of A.
std
S = std(A) returns the standard deviation of the elements of A along the first array
sum
S = sum(A) returns the sum of the elements of A along the first array dimension whose size does not equal 1.
exp
Y = exp(X) returns the exponential ex for each element in array X. For complex elements z = x + iy, it returns the complex exponential
log
Y = log(X) returns the natural logarithm ln(x) of each element in array X.
polyfit
p = polyfit(x,y,n) returns the coefficients for a polynomial p(x) of degree n that is a best fit (in a least-squares sense) for the data in y. The coefficients in p are in descending powers, and the length of p is n+1
meshgrif
p = polyfit(x,y,n) returns the coefficients for a polynomial p(x) of degree n that is a best fit (in a least-squares sense) for the data in y. The coefficients in p are in descending powers, and the length of p is n+1
loglog
loglog(Y) plots the columns of Y versus their index if Y contains real numbers. If Y contains complex numbers, loglog(Y) and loglog(real(Y),imag(Y)) are equivalent. loglog ignores the imaginary component in all other uses of this function.
• Semilogx
semilogx plot data as logarithmic scales for the x-axis.
T = readtable(filename) creates a table by reading column oriented data from a file.
readtable determines the file format from the file extension:
num = xlsread(filename) reads the first worksheet in the Microsoft® Excel® spreadsheet workbook named filename and returns the numeric data in a matrix.
sound
sound(y) sends audio signal y to the speaker at the default sample rate of 8192 hertz.
example
sound(y,Fs) sends audio signal y to the speaker at sample rate Fs.
example
sound(y,Fs,nBits) uses nBits bits per sample for audio signal y.
beep
beep produces your computer’s default beep sound, if it is enabled.