Matlab

Matlab #

Matlab is a handy tool for image processing for the following reasons:

  • Is easy to learn and use: Its syntax is simple, intuitive, and close to everyday language, making it accessible for beginners.
  • Provides an integrated environment: All necessary tools and functionalities are within a single platform.
  • Supports rapid development: Prototyping and experimentation can be done quickly, speeding up the development process.
  • Is platform-independent: It works across Windows, macOS, and Linux systems.
  • Includes extensive support and documentation: MATLAB is backed by detailed documentation, tutorials, and a large community of users.

Essential MATLAB Commands Cheat Sheet #

Here’s a quick reference for some of the most commonly used MATLAB commands, grouped by category.

General Commands #

CommandDescription
clcClears the command window.
clearClears all variables from the workspace.
close allCloses all figure windows.
helpProvides help for a specific function or command.
docOpens the documentation for more details.
loadLoads variables from a .mat file.
saveSaves variables to a .mat file.

Data Creation and Management #

CommandDescription
[a, b; c, d]Creates a matrix.
zeros(m, n)Creates an m x n matrix of zeros.
ones(m, n)Creates an m x n matrix of ones.
eye(n)Creates an n x n identity matrix.
size(A)Returns the size of matrix A.
length(A)Returns the length of A (longest dimension).
reshape(A, m, n)Reshapes A into m x n matrix.

Indexing and Operations #

CommandDescription
A(i, j)Accesses element at ith row and jth column.
A(:, j)Accesses all rows in the jth column.
A(i, :)Accesses all columns in the ith row.
A'Transposes matrix A.
inv(A)Calculates the inverse of A.
sum(A)Sums elements in each column of A.
mean(A)Calculates the mean of each column in A.

Plotting and Visualization #

CommandDescription
plot(x, y)Creates a line plot of y vs. x.
title('Title')Adds a title to the current plot.
xlabel('X-axis')Labels the x-axis.
ylabel('Y-axis')Labels the y-axis.
imshow(img)Displays an image.
subplot(m, n, p)Creates a subplot grid of m by n and selects p.

Image Processing #

CommandDescription
imread('file.png')Reads an image file into a matrix.
imwrite(A, 'file.png')Writes matrix A to an image file.
rgb2gray(img)Converts an RGB image to grayscale.
imresize(img, scale)Resizes the image by a scale factor.

Conditional Statements and Loops #

CommandDescription
if conditionBegins an if statement.
elseAdds an alternative path in an if statement.
for i = 1:nBegins a for loop from 1 to n.
while conditionBegins a while loop that runs while condition is true.
breakExits a loop immediately.
continueSkips to the next loop iteration.

File I/O #

CommandDescription
fopen('file.txt', 'r')Opens a file for reading ('w' for writing).
fread / fwriteReads/writes data in binary form.
fprintfWrites formatted data to a file.
fclose(fileID)Closes the opened file.

Data classes #

NameDescription
DoubleDouble-precision, floating-point number in the range approximately -10^308 to 10^308 (8 bytes)
Uint8Unsigned 8-bit integers in the range [0, 255] (1 byte)
Uint16Unsigned 16-bit integers in the range [0, 65,535] (2 bytes)
Uint32Unsigned 32-bit integers in the range [0, 4,294,967,295] (4 bytes)
Int8Signed 8-bit integers in the range [-128, 127] (1 byte)
Int16Signed 16-bit integers in the range [-32,768, 32,767] (2 bytes)
Int32Signed 32-bit integers in the range [-2,147,483,648, 2,147,483,647] (4 bytes)
SingleSingle-precision floating-point number in the approximate range -10^308 to 10^308 (4 bytes)

Matrix operations #

NameMATLAB operator or function
Matrix transposeapostrophe (‘) operator
Inversioninv function
Matrix determinantdet function
Flip up and downflipud function
Flip left and rightfliplr function
Matrix rotationrot90 function
Matrix reshapereshape function
Sum of the diagonal elementstrace function

Standard arrays #

NameDescription
zeros(m,n)Creates an m-by-n matrix of zeros
ones(m,n)Creates an m-by-n matrix of ones
true(m,n)Creates an m-by-n matrix of logical ones
false(m,n)Creates an m-by-n matrix of logical zeros
eye(n)Returns an n-by-n identity matrix
magic(m)Returns a magic square of order m
rand(m,n)Creates an m-by-n matrix with entries uniformly distributed in the interval [0,1]
randn(m,n)Creates an m-by-n matrix with entries from a normal (Gaussian) distribution with mean 0 and variance 1