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
#
Command | Description |
---|
clc | Clears the command window. |
clear | Clears all variables from the workspace. |
close all | Closes all figure windows. |
help | Provides help for a specific function or command. |
doc | Opens the documentation for more details. |
load | Loads variables from a .mat file. |
save | Saves variables to a .mat file. |
Data Creation and Management
#
Command | Description |
---|
[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
#
Command | Description |
---|
A(i, j) | Accesses element at i th row and j th column. |
A(:, j) | Accesses all rows in the j th column. |
A(i, :) | Accesses all columns in the i th 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
#
Command | Description |
---|
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
#
Command | Description |
---|
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
#
Command | Description |
---|
if condition | Begins an if statement. |
else | Adds an alternative path in an if statement. |
for i = 1:n | Begins a for loop from 1 to n . |
while condition | Begins a while loop that runs while condition is true. |
break | Exits a loop immediately. |
continue | Skips to the next loop iteration. |
File I/O
#
Command | Description |
---|
fopen('file.txt', 'r') | Opens a file for reading ('w' for writing). |
fread / fwrite | Reads/writes data in binary form. |
fprintf | Writes formatted data to a file. |
fclose(fileID) | Closes the opened file. |
Data classes
#
Name | Description |
---|
Double | Double-precision, floating-point number in the range approximately -10^308 to 10^308 (8 bytes) |
Uint8 | Unsigned 8-bit integers in the range [0, 255] (1 byte) |
Uint16 | Unsigned 16-bit integers in the range [0, 65,535] (2 bytes) |
Uint32 | Unsigned 32-bit integers in the range [0, 4,294,967,295] (4 bytes) |
Int8 | Signed 8-bit integers in the range [-128, 127] (1 byte) |
Int16 | Signed 16-bit integers in the range [-32,768, 32,767] (2 bytes) |
Int32 | Signed 32-bit integers in the range [-2,147,483,648, 2,147,483,647] (4 bytes) |
Single | Single-precision floating-point number in the approximate range -10^308 to 10^308 (4 bytes) |
Matrix operations
#
Name | MATLAB operator or function |
---|
Matrix transpose | apostrophe (‘) operator |
Inversion | inv function |
Matrix determinant | det function |
Flip up and down | flipud function |
Flip left and right | fliplr function |
Matrix rotation | rot90 function |
Matrix reshape | reshape function |
Sum of the diagonal elements | trace function |
Standard arrays
#
Name | Description |
---|
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 |