0% found this document useful (0 votes)
129 views18 pages

MATLAB Scalars and Matrices Guide

This document provides an overview of basic scalars, matrices, and matrix operations in MATLAB. It discusses how to create matrices using various syntax, perform transpose operations, access elements and submatrices, and use special matrices like identity and random matrices. It also summarizes logical operations, common mathematical functions, writing functions, programming logic, function parameters, calling functions, scripts, and basic plotting.

Uploaded by

Zachary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views18 pages

MATLAB Scalars and Matrices Guide

This document provides an overview of basic scalars, matrices, and matrix operations in MATLAB. It discusses how to create matrices using various syntax, perform transpose operations, access elements and submatrices, and use special matrices like identity and random matrices. It also summarizes logical operations, common mathematical functions, writing functions, programming logic, function parameters, calling functions, scripts, and basic plotting.

Uploaded by

Zachary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Basic Scalars/Matrices

• For MATLAB a scalar is simply a 1-by-1 matrix.

• To create a matrix:
1 2 3
A = [1 2 3; 4 5 6]; makes
4 5 6
• This also works:
A = [1,2,3;4,5,6]; or [1 2 3
4 5 6]

• The ‘ symbol denotes transpose:


if A = [1, 2, 3; 4, 5, 6] then A′ = [1, 4; 2, 5; 3, 6]
More matrices
• You can form a matrix out of a number of vectors.
• a = [1 2 3]; b = [4 5 6];

• A = [a b]; gives 1 2 3 4 5 6

1 2 3
• A = [a; b]; gives
4 5 6

• Accessing a single element: A(1, 2) for the above gives 1st


row, 2nd column element = 2
Using the : symbol
• : is used either in declaration or accessing vectors/matrices

• Declaration format: start:stride:end

• A = [0:5:20]; makes 0 5 10 15 20

• Use transpose to make column vectors

0
5
A = [0:5:20]’; makes 10
15
20
Using the : symbol
• Access format: Similar, b

1 2 3 4 2
5 6 7 8 6
A= A(:, 2) gives 2nd column
9 10 11 12 10
13 14 15 16 14

3 4
A(1:2, 3:4) gives 1-2 row, 3-4 column submatrix
7 8

Starting row is 1, ending row can be end. Can use stride


here too, but not very useful.
Special Matrices
• eye(n) is the identity matrix of size n x n.

• zeros(m, n) is the m x n matrix with only zeroes.

• ones(m, n) is the m x n with only 1’s.

• magic(n) gives a n x n matrix with integer coefficients from


1 to n² with equal column and row sums.
Random Matrices
• rand(m, n) is a matrix of size m by n with independent
entries that are uniformly distributed on the interval [0, 1]

• randn(m, n) is a matrix of size m by n with independent


entries that are normally distributed

• rand(n) and randn(n) return square matrices of size n by


n.
Matrix Operations
• Add, subtract, divide, multiply, exponent: + - \ / * ˆ

• * and \ correspond to matrix product and multiplication by


the inverse:
𝐴 ∗ 𝐵 = 𝐴𝐵, 𝐴⁄𝐵 = 𝐴𝐵23, 𝐴\B = 𝐴23𝐵

• The same operations (except \) are available component


wise:
[1, 2, 3]. * [2, 1, 2] = [2, 2, 6]

• A\b solves the linear system Ax = b.


Matrix Operations cont.
• null(A) is an orthogonal basis for the null space of A

•sum(A) returns a row vector containing the sum of the


columns of A.
Logical Operations
• Tests such as A < b return logical values

• These can be manipulated as regular integers (1 for true,


0 for false).

• find will return all the elements for which a condition is


true:
find([1, 2, 3] > 1) returns [2, 3]
Logical Operations cont.
• [v, id] = max(a) returns the maximum element of the
vector a and the corresponding indices in id.

• [s, id] = sort(a) returns the elements of a sorted in


ascending order and the permutation id such that s(id) is
increasing.
Usual Functions
• Mathematics: sin, cos, exp, log, log10, sqrt, ceil, floor,
round, ...

• Information: size, length, who, whos, ls

• Management: save, load, clear


save filename x y A
load filename
Writing functions
• File -> new -> function

• Functions/scripts/classes are all .m files, but different


semantics. To be able call functions, place them in your
project directory.

function [ output_args ] = Silly( input_args )


%SILLY Summary of this function goes here
% Detailed explanation goes here

end
Programming Logic
• if, else statements:

if (a > 1)
blah
else
blahblah
end
• for statements can be used too:
for i=1:n
moreblah
end
• Similar behavior for repeat, until, while, etc.
Function parameters
function [ output1, output2 ] = Silly( input1, input2)
• To input values, use the as many arguments after the
function name as you need, then use them in your
program.
some_value = input1*input2;

• Output values must be set before the “end” statement.

output1 = some_value;
output2 = 15.7;
end
Calling Functions
• Note that the type of input1, input2 is not set anywhere.
Can be scalars, vectors, matrices…
• To call this function with 2 return values, do:
[a, b] = Silly(5, 7);

[a, b] = Silly(vector1, vector2);

• This will save output1 as a and output2 as b.

• If we specify fewer return parameters, the first few are


used.
Scripts
• You should write all you commands in a script using the
editor.

• Use F5 to run the script. Using the name of the script from
the command line works too.
• Use F9 to run the current selection.

• CTRL-i will automatically (and correctly) indent the current


selection.
• CTRL-R will comment the current selection, CTRL-T will
uncomment it (useful to test only parts of a code).
Plotting
• plot(x, y) will plot a function that takes values
y = (y1, . . . , yn) at the points x = (x1, . . . , xn).

• Use xlabel(′ALabelForX′) and ylabel(′ALabelForY ′) to put


labels on the axes and Title(′ATitle′) to include a title.

• plot(x1, y1, ':bo', x2, y2, '-r.') will plot two curves, one as a
blue dotted line with circles at each point, the other red
continuous with dots.
Plotting cont.
• Look for ”Linespec” in the MATLAB documentation to find
other codes for line colors, markers, etc.

• Use legend(′plot1′,′ plot2′, ...) to include a legend.

• To combine plots: use hold on after the first one and hold
off after the last plot.

hold on
plot (x1, y1, ':bo')
plot (x2, y2, '-r.')
hold off

You might also like