MATLAB Flashcards

1
Q

matlab install direcory?

A

Type, matlabroot in command window.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

matlab: three dots … meaning?

A

Line continuation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

matlab: How to measure elapsed time?

A

using matlab ‘tic’ and ‘toc’.
‘tic’ starts a stopwatch. ‘toc’ gives the elapsed time in seconds.
‘tic’ will reset the stopwatch.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do u find type of a variable in matlab?

A

class(varName)

— or —

whos your_variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

matlab: find dimentions of an array / matrix

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to get info about machine architecture on which matlab is running on?

A

Type ‘computer’ on command window.

> [str, maxsize, endian] = computer;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does a call to dsp.SineWave(…) return?

A

A column vector of sine wave samples, containing ‘SamplesPerFrame’ samples.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does randi() function do?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to draw scatter plot in matlab?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

matlab: How to do “Phase Shift Keying” (PSK) for given baseband data.

A

Use pskmod()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does cat/concatenate of arrays/matrices work in matlab?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

matlab:
x = 23;
what is type of x?

A

double

This is default of numeric variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

matlab:
How do you create an uint16 type variable?

A

x = uint16(78);

other variable types are int16, uint8, int32, uint64,
single , double etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

matlab: how to find minim value in an array?

A

min(array_name);

max is also available.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

matlab: How to get absolute values of an array X?

A

y = abs(X);

y will have same dimensions as X, but each element will have absolute value of X elem. No negatives.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Matlab: How to find mean of an array X?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Matlab: How do u open a file?

A

[fd, err_msg] = fopen(‘abc.data’, ‘r’);

data = fread(fd, inf, ‘int16’);
data will be a row vector of int16.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

matlab: what is the last element of the array X.

A

X(end)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Give some examples of array slicing in matlab.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Matlab: What happens when you apply a single index to a matrix?

A

Matrix is considered a tall column vector.

21
Q

ML: How to find if a variable is number/scalar or an array/matrix?

A

isscalar(x)

22
Q

Matlab: How to temporarily add a search path in matlab?

A

addpath(‘/new/path/dir’);

23
Q

Matlab: how to comment out a block of lines?

A

Enclose block of lines with

%}

24
Q

How to record matlab activity in a file (diary file)

A

diary abc.txt
disp(‘abc’)
diary off

25
Q

matlab: How to read a CSV file into a matrix?

A

readmatrix(‘file_name.csv’);

26
Q

Matlab: what is difference between single quoted characters and double quoted characters?

A

Single quoted characters create a ‘character array’.

Double quoted characters create strings.

27
Q

Matlab: how to do u create tables?

A

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

28
Q

Matlab: How different columns of a table can be accessed?

A

If column name or variable name is a string without spaces,
tbl.varName

if column name contains spaces,
tbl.(‘var name’);

29
Q

Matlab: Undo ?

A

CTL + Shift + minus

30
Q

Matlab: How to append at the end of an arry?

A

array name = arr

For row vector,
arr = [arr, new_val]

For column vector,
arr = [arr ; new_val]

31
Q

Matlab: Duplicate a line?

A

CTL + SHIFT + c

32
Q

Matlab: How to suppress the output of a command to console?

A

use evalc,
evalc(‘ command here ‘);
remember to use quotes around command.

evalc(‘fprintf(‘‘Hello, World!’’)’);

33
Q

Matlab: Can you add fields to structs dynamically, while program is running?

A

yes,

sruct.new_field = new_val;

34
Q

Matlab: how to make sure a specific struct indeed has a specific field?

A

isfield(struct_name, field_name)

35
Q

Matlab: How to create a struct with given set of field, field_value pairs?

A

x = struct(‘field_name1, field-val_1, field_name2, field_val_2, …)

36
Q

Matlab: What is ‘nargin’?

A

Inside a function, it is the number actual number of arguments that the function is called with.

37
Q

Matlab: What is ‘varargin’?

A

Inside a function, it is a cell array containing all arguments passed to the function.

38
Q

Matlab: What is function handle?

A

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)

39
Q

Matlab: What does deal function do?

A

It distributes all of its input arguments to receiver arguments,

eg,

[a, b] = deal(7, 10);

Useful in initializing cell arrays with known values.

40
Q

matlab: How does max() work on an array.

A

[a, b] = max(arr)

a = max value
b = index of max value

41
Q

matlab: What are sub functions and nested functions?

A

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.

42
Q

matlab: What are ways to print error/warning messages?

A

error(“Error message”)
Halts the program.

warning(“Warning message”);
Does not halt the program. Prints line number automatically.

43
Q

matlab: how to get documentation and help about a function, object, class?

A

On command line, type

doc mean

help median

44
Q

matlab: How to get information about a table (tbl)?

A

tbl.Properties
tbl.Properties.VariableNames
height(tbl)

45
Q

matlab: How do you define a class?

A

Using ‘classdef’

With sections
properties
methods

46
Q

Matlab: How to list properties of a class?

A

properties(class_name);

47
Q

Matlab: How to find all methods of a class?

A

methods(class_name);
methodsview(class_name);

48
Q

Matlab: How to get total num of elements in a array or matrix?

A

numel(mtx_name);
numel(array_name);

size(mtx_name) gives dimensions.