Warning
This page is located in archive. Go to the latest version of this course pages.

Matlab Help for Typical Situations in Homeworks

Some recommendations for the fastest progress

  1. Do not trust any results unless you verified them.
  2. Always verify your results.
  3. Try to visualize your results.
  4. Each Matlab command has help. E.g. for plot
    help plot % text version
    doc plot % html version
Debugging

Matlab has a powerful debugger. Either put breakpoints everywhere you want, or use the command keyboard in your code to stop processing at the place. Exit the debugging mode:

dbquit

You can also start debugging automatically in case of error by turning on

dbstop error
Saving intermediate data

Especially when some user input is needed, it is wise to store intermediate data for later reuse. Following example could be helpful

if( ~exist( 'my_points.mat', 'file' ) )
  [x y] = ginput( 7 );
  save( 'my_points.mat', 'x', 'y' );
else
  load( 'my_points.mat' );
end

Vectors and its transformations

Vectors are stored as column matrices.

u = [1;2];
X = [1;2;3];
Xmore = [1 2 3; 4 5 6]';  % transpose at the end

Vectors are multiplied by a matrices on the left:

P = [1 0 0 -5; 0 1 0 -6; 0 0 1 1];  % a camera
ux_homog = P * [X;1]; % projection

Special matrices: Identity, Zeros, Ones

I = eye(3); % 3x3 identity
I2 = diag( [1 1 1] ); % same result
Z = zeros(3); % 3x3 matrix of zeroes
O = ones(3); % 3x3 matrix of ones

Euclidean distance of two 3D points

X1 = [1;2;3];
X2 = [4;5;6];
d2 = norm(X1-X2); % cannot be used for more vectors
d2 = sqrt(sum((X1-X2).^2)); % for matrices with points stored in columns

Dot (scalar) product

u1 = [4;5;1];
u2 = [7;8;1];
dot12 = u1' * u2; % apostrophe is the transpose

Element-by-element matrix operations

In addition to standard matrix addition and subtraction, there are some operators working element-by-element.

M1 = [1 2; 3 4; 5 6];
M2 = [7 8; 9 10; 11 12]; % note the sizes
A = M1 .* M2; % Hadamard product (multiplication element-by-element; note the dot)
B = M1 ./ M2; % Hadamard division (division element-by-element)
C = M1 .^ 2;  % power element-by-element

Selection of the real part of complex numbers

numbers = [1 2 sqrt(-3) acos(2) 4 6+3i 7 ];
is_real = imag( numbers ) == 0; % logical array
real_only = numbers( is_real );

Cross product

u1 = [4;5;1];
u2 = [7;8;1];
u12x = cross(u1,u2);
% verification of orthogonality by dot product 
% (remember the recommendations 1 and 2)
u12x' * u1 % should be zero
u12x' * u2 % should be zero

Angle given x and y coordinate.

Let x = r cos(a) and y = r sin(a). Then the angle can be recovered as

a = atan2( y, x );

This function is safe when either of its arguments is zero (but not both), and works for four quadrants.

Capturing a bitmap snapshot of a figure/axis

Let hnd be some axes handle (e.g. gca) or some figure handle (e.g. gcf). The snapshot identical to the (pixel-wise) appearance of the screen is captured as

f = getframe( hnd );
img = f.cdata;

Notes: when capturing a sequence, do not forget to call drawnow before capturing. Also be careful not to obscure the captured window by other GUI content, mouse pointer, etc.

Creating a video sequence

mov = avifile( 'my_file.avi' );
mov.compression = 'none';
mov.fps = 25; % or whatever you want
  
% now iterate creation of frames
% all frames must have the same size
mov = addframe( mov, img ); 

% end of frame creation 

mov = close( mov );

Note that video compression (compression different from 'none') usually does not work well, so preferred way is to store raw sequence and compress later.

To be continued ...

courses/gvg/labs/gvg-hw-help.txt · Last modified: 2019/01/19 18:22 (external edit)