[Basic Matrix Operations]

Date: Nov 2011
Tags: Matlab

I put this together when I revisited working in Matlab as a reference and a quick look up of mostly used functions. All of these are compatible with Octave as well.

Creating matrices

% Produce 2x3 matrix of ones
A = ones(2,3)

% Produce 2x3 matrix of twos
A = 2*ones(2,3)

% Produce 2x3 matrix of 0s
A = zeros(2,3)

% Produce 2x3 matrix of random numbers
A = rand(2,3)

% Produce 2x3 matrix of gaussian numbers with mean 0 and variation 1
A = randn(2,3)

% Produce large vector of gaussian numbers
A = -6 + sqrt(10)*(randn(1,10000));

% Print histogram

% Produce 3x3 matrix of random numbers from <-1;1>
A = rand(3,4);

% 5x5 identity matrix
I = eye(5)

% Magic square (all columns, rows, diagonals sum up to same number)
MAGIC = magic(3)

Operations on matrices

% Everything in 2nd column
A (:,2)

% Everyting from 1st and 3rd row, all columns
A([1 3],:)

% Append another column vector
A = [A, [100; 101; 102; 103]]

% All elements of A into single column vector
B = A(:)

% Invert matrix
C = pinv(A)

% Transpose matrix
D = C'

% Element-wise multiplication (works with other signs too):
E = rand(2,3)
F = 2*ones(2,3)
G = E .* F

% ElementwiseLogarithm of matrix

% Elementwise Exponentiaion

% Element wise minus E = -E

% Elementwise Absolute values

% Elementwise adding
e = [2;4;5;7]
H = e + ones(length(e),1)
I = E + 1

% Max value of vector
a = [1 34 6 2]
val = max(a)
[val, index] = max(a)

% Element-wise comparison (returns true/false)
a < 3

% Element-wise comparison (returnse elements)
find(a < 3)

% Add up elements
summation = sum(a)

% Mulitply elements
product = prod(a)

% Floor elements
FLOORING = floor(a)

% Ceil elements
CEILING = ceil(a)

% Column- and row- wise maximums where last argument is dimension
columnWiseMaximums = max(A,[],1)
rowWiseMaximums = max(A,[],2)
maximumElement = max(A(:))

% SumUp elements to create vector
sumOfColumns = sum(MAGIC,1)
sumOfRows = sum(MAGIC,2)

% Diagonal sum, first multiply by identity matrix to make all
% elements expect of diagonal 0
MAGIC .* eye(3)
sumOfDiagonal = sum(sum(MAGIC.*eye(3)))

Measuring matrices

% Size of matrix
S = size(A)

% Size of rows
S = size(A,1)

% Size of columns
S = size(A,2)

% Size of longer dimension
S = length(A)

{Please enable JavaScript in order to post comments}


Robot swarms in action

Watch e-puck robots collect resources and bring them back to base. While the previous simulation work helped us to learn a lot about the...

[read full]

How to compile code for e-puck robots on your computer

Compiling code natively on e-puck robots or cross-compiling on your computer can be very tedious. Luckily, there is a third option:...

[read full]

Coding for tomorrow: Why is good code important?

"Why should I care about how my code is written, as long as it works?" I will argue here that well-structured and well-written code not...

[read full]

How Coding in Python Might Be Bad For You

7 reasons why coding in Python is like writing a really bad essay and getting away with it

[read full]