0% found this document useful (0 votes)
94 views

Throughout This Document X and y Will Be Either Row or Column Vectors and A Will Always Be A Matrix

This document provides a cheat sheet for MATLAB. It includes sections on basics, operations on matrices and vectors, defining and changing variables, basic arithmetic and functions, constructing matrices and vectors, for loops, while loops, logicals, plotting, and function handles. The cheat sheet lists common MATLAB commands and functions with brief explanations and examples.

Uploaded by

Alexis marcial
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

Throughout This Document X and y Will Be Either Row or Column Vectors and A Will Always Be A Matrix

This document provides a cheat sheet for MATLAB. It includes sections on basics, operations on matrices and vectors, defining and changing variables, basic arithmetic and functions, constructing matrices and vectors, for loops, while loops, logicals, plotting, and function handles. The cheat sheet lists common MATLAB commands and functions with brief explanations and examples.

Uploaded by

Alexis marcial
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

MATLAB CHEAT SHEET

Throughout this document x and y will be either row or column vectors and A will always be a matrix.

Basics Operations on Matrices and Vectors


clc Clear command window 3 * x Multiply every element of x by 3
clear Clear all variables x + 2 Add 2 to every element of x
clf Clear all plots x + y Element-wise addition of two vectors x and y
close all Close all plots A * y Product of a matrix and vector
doc function Open help page for function A * B Product of two matrices
% This is a comment Comments A .* B Element-wise product of two matrices
ctrl-c Abort the current operation A ^ 3 Square matrix A to the third power
format short Display 4 decimal places A .^ 3 Every element of A to the third power
format long Display 15 decimal places cos(A) Compute the cosine of every element of A
disp('text') Print text abs(A) Compute the absolute values of every element of A
A' Transpose of A
inv(A) Compute the inverse of A
Defining and Changing Variables det(A) Compute the determinant of A
a = 3 Define variable a to be 3 eig(A) Compute the eigenvalues of A
x = [1, 2, 3] Set x to be the row vector [1, 2, 3] size(A) Get the size of A
x = [1; 2; 3] Set x to be the column vector [1, 2, 3]T
A = [1, 2, 3, 4; Set A to be a 3 × 4 matrix
Entries of Matrices and Vectors
x(2:12) The 2nd to the 12th elements of x
5, 6, 7, 8;
x(2:end) The 2nd to the last elements of x
9, 10, 11, 12]
x(1:3:end) Every third element of x from the first to last
x(2) = 7 Change x from [1, 2, 3] to [1, 7, 3]
A(5,:) Get the 5th row of A
A(2,1) = 0 Change A2,1 from 5 to 0
A(:,5) Get the 5th column of A
A(5, 1:3) Get the first to third elements in the 5th row

Basic Arithmetic and Functions Plotting


3*4, 7+4, 2-6, 8/3 multiply, add, subtract and divide
plot(x,y) Plot y versus x (must be the same
3^7 Compute 37 length)

sqrt(5) Compute 5 loglog(x,y) Plot y versus x on a log-log scale
log(3) Compute ln(3) (both axes have a logarithmic scale)
log10(100) Compute log10 (100) semilogx(x, y) Plot y versus x with x on a log scale
abs(-5) Compute | − 5| semilogy(x, y) Plot y versus x with y on a log scale
sin(5*pi/3) Compute sin(5π/3) axis equal Force the x and y axes to be scaled
equally
floor(3.8) Compute ⌊3.8⌋
title('A Title') Add a title to the plot
xlabel('x label') Add a label to the x axis
ylabel('y label') Add a label to the y axis
Constructing Matrices and Vectors
legend('foo', 'bar') Label 2 curves for the plot
zeros(12, 5) Make a 12 × 5 matrix of zeros
grid Add a grid to the plot
ones(12, 5) Make a 12 × 5 matrix of ones
hold on Multiple plots on single figure
eye(5) Make a 5 × 5 identity matrix
figure Start a new plot
eye(12, 5) Make a 12 × 5 identity matrix
linspace(1.4, 6.3, 1004) Make a vector with 1004 ele- Constants
ments evenly spaced between pi π = 3.141592653589793
1.4 and 6.3
NaN Not a number (i.e. 0/0)
logspace(1.4, 6.3, 1004) Make a vector with 1004 ele-
ments where the log of the Inf Infinity
spacing is evenly increasing be- realmax Largest positive floating-point number 1.7977 · 10308
tween 1.4 and 6.3 realmin Smallest positive floating-point number 2.2251 ·
7:15 Row vector of 7, 8, . . . , 14, 15 10−308

Steven E. Thornton steventhornton.ca Updated: December 16, 2016


MATLAB CHEAT SHEET

For loops Function Handles


for k = 1:5 f = @(x) sin(x.^2)./(5*x);
disp(k);
end f(pi/2)
0.0795
f([-pi/2, 0, pi/2])
-0.0795 NaN 0.0795
While loops

k = 0; Plotting
while k < 7
k = k + 1;
end x = linspace(-3*pi, 3*pi, 1000);
y1 = sin(x);
y2 = cos(x);

Logicals plot(x, y1, 'k-'); % Plot sin(x) as a black line


hold on % Now we can add another curve
plot(x, y2, 'r-'); % Plot cos(x) as a red line
a = 10; % Assign a the value of 10
a == 5 % Test if a is equal to 5 % Set the axis limits
false axis([-3*pi, 3*pi, -1.5, 1.5])
a == 10 % Test if a is equal to 10
true % Add axis labels
a >= 5 % Test if a is greater than or equal to 5 xlabel('x');
true ylabel('y');
a < 11 % Test if a is less than 11
true % Add a title
a ~= 4 % Test if a is not equal to 4 title('A plot of cos(x) and sin(x)');
true
a > 1 && a ~= 10 % Test if a is greater than 1 AND % Add a legend
false % not equal to 10 legend('sin(x)', 'cos(x)');
a > 1 || a ~= 10 % Test if a is greater than 1 OR
true % not equal to 10

A plot of cos(x) and sin(x)


1.5
Conditional Statements sin(x)
cos(x)
1
if a > 10
disp('Greater than 10');
elseif a == 5
0.5
disp('a is 5');
else
disp('Neither condition met');
0
y

end

-0.5
Functions
-1
function output = addNumbers(x, y)
output = x + y;
end
-1.5
-5 0 5
addNumbers(10, -5)
5 x

Steven E. Thornton steventhornton.ca Updated: December 16, 2016

You might also like