MATLAB Flashcards
matlab install direcory?
Type, matlabroot in command window.
matlab: three dots … meaning?
Line continuation.
matlab: How to measure elapsed time?
using matlab ‘tic’ and ‘toc’.
‘tic’ starts a stopwatch. ‘toc’ gives the elapsed time in seconds.
‘tic’ will reset the stopwatch.
How do u find type of a variable in matlab?
class(varName)
— or —
whos your_variable
matlab: find dimentions of an array / matrix
size(your_array)
size(your_matrix)
class(your_matrix) gives the type of data elements contained in the matrix.
A column vector has N rows and one column. Dimension N x 1
A row vector has one row and N columns. Dimension 1 x N
whos your_array
gives all info (size, type) of array.
To get total num of elements
numel(your_matrix);
How to get info about machine architecture on which matlab is running on?
Type ‘computer’ on command window.
> [str, maxsize, endian] = computer;
What does a call to dsp.SineWave(…) return?
A column vector of sine wave samples, containing ‘SamplesPerFrame’ samples.
What does randi() function do?
Returns a matrix of random nums, in a given range, in a matrix of given shape.
randi(99, 1, 4): returns a 1x4 array of ints 1 … 99
randi([10, 20], 3, 4): returns a 3x4 matrix of ints 10 … 20
randi([10, 20], 3): returns a 3x3 square matrix of ints 10 … 20
randi([10, 20], ): returns one integer between 10 and 20
randi(range, shape)
How to draw scatter plot in matlab?
scatter([1 2 3], [1 4 9]);
scatter([1 2 3], [1, 8, 27], ‘*’, ‘r’)
scatter() needs both X & Y data arrays.
scatterplot() can work with only Y data array, X array is inferred.
matlab: How to do “Phase Shift Keying” (PSK) for given baseband data.
Use pskmod()
How does cat/concatenate of arrays/matrices work in matlab?
x = cat(dim, arr1, arr2);
dim is the dimension that will increase after matrices are put together.
A row array is of dimension 1xn
To cat two row arrays, dim=2, since num of columns will increase.
Two cat two column vectors, use dim=1, since number of rows will increase.
matlab:
x = 23;
what is type of x?
double
This is default of numeric variable.
matlab:
How do you create an uint16 type variable?
x = uint16(78);
other variable types are int16, uint8, int32, uint64,
single , double etc.
matlab: how to find minim value in an array?
min(array_name);
max is also available.
matlab: How to get absolute values of an array X?
y = abs(X);
y will have same dimensions as X, but each element will have absolute value of X elem. No negatives.
Matlab: How to find mean of an array X?
If X is a row or column vector,
mean(X) returns mean value.
If X is a matrix,
mean(X) returns an row vector, containing mean of each column.
Matlab: How do u open a file?
[fd, err_msg] = fopen(‘abc.data’, ‘r’);
data = fread(fd, inf, ‘int16’);
data will be a row vector of int16.
matlab: what is the last element of the array X.
X(end)
Give some examples of array slicing in matlab.
X(3 : 7)
X(5 : end)
X(1:2:end) % All odd index elements.
Step size is in the middle.
If the step size is negative, successive indices decrease.
X(end: -1: 1) % Reverse the array.
For matrices
one or both indices can be ranges,
A(2:4, 1:2)
A single : indicates 1:end
A(3,:) % Third row, all cols.
A(: , end) % last column
Matlab: What happens when you apply a single index to a matrix?
Matrix is considered a tall column vector.
ML: How to find if a variable is number/scalar or an array/matrix?
isscalar(x)
Matlab: How to temporarily add a search path in matlab?
addpath(‘/new/path/dir’);
Matlab: how to comment out a block of lines?
Enclose block of lines with
%}
How to record matlab activity in a file (diary file)
diary abc.txt
disp(‘abc’)
diary off
matlab: How to read a CSV file into a matrix?
readmatrix(‘file_name.csv’);
Matlab: what is difference between single quoted characters and double quoted characters?
Single quoted characters create a ‘character array’.
Double quoted characters create strings.
Matlab: how to do u create tables?
Use function ‘table’. Provide it with column arrays of same sizes.
T = table(name_vec, age_vec);
Table column names are same as input vector names.
height(T) returns the number of rows in table
Matlab: How different columns of a table can be accessed?
If column name or variable name is a string without spaces,
tbl.varName
if column name contains spaces,
tbl.(‘var name’);
Matlab: Undo ?
CTL + Shift + minus
Matlab: How to append at the end of an arry?
array name = arr
For row vector,
arr = [arr, new_val]
For column vector,
arr = [arr ; new_val]
Matlab: Duplicate a line?
CTL + SHIFT + c
Matlab: How to suppress the output of a command to console?
use evalc,
evalc(‘ command here ‘);
remember to use quotes around command.
evalc(‘fprintf(‘‘Hello, World!’’)’);
Matlab: Can you add fields to structs dynamically, while program is running?
yes,
sruct.new_field = new_val;
Matlab: how to make sure a specific struct indeed has a specific field?
isfield(struct_name, field_name)
Matlab: How to create a struct with given set of field, field_value pairs?
x = struct(‘field_name1, field-val_1, field_name2, field_val_2, …)
Matlab: What is ‘nargin’?
Inside a function, it is the number actual number of arguments that the function is called with.
Matlab: What is ‘varargin’?
Inside a function, it is a cell array containing all arguments passed to the function.
Matlab: What is function handle?
It is a variable containing a function name.
Function can be called using this variable.
fh = @function_name
call the function
fh(a, b, c)
Matlab: What does deal function do?
It distributes all of its input arguments to receiver arguments,
eg,
[a, b] = deal(7, 10);
Useful in initializing cell arrays with known values.
matlab: How does max() work on an array.
[a, b] = max(arr)
a = max value
b = index of max value
matlab: What are sub functions and nested functions?
Sub-function is a func defined in a a .m file after the definition of the main function. Sub-func is invisible outside its .m file.
Nested func is defined inside the body of a function.
matlab: What are ways to print error/warning messages?
error(“Error message”)
Halts the program.
warning(“Warning message”);
Does not halt the program. Prints line number automatically.
matlab: how to get documentation and help about a function, object, class?
On command line, type
doc mean
help median
matlab: How to get information about a table (tbl)?
tbl.Properties
tbl.Properties.VariableNames
height(tbl)
matlab: How do you define a class?
Using ‘classdef’
With sections
properties
methods
Matlab: How to list properties of a class?
properties(class_name);
Matlab: How to find all methods of a class?
methods(class_name);
methodsview(class_name);
Matlab: How to get total num of elements in a array or matrix?
numel(mtx_name);
numel(array_name);
size(mtx_name) gives dimensions.