**Disclaimer:**this post contains no concrete references, examples, excerpts or solutions for any of the Coursera courses, exercises, or homework assignments.### Matrix Basics

Suppose we have a 3 by 5 matrix**like this:**

*A*octave:> A = [1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7]

A =

1 2 3 4 5

2 3 4 5 6

3 4 5 6 7

Then to extract a single element from

**is just like in most other languages:**

*A*octave:> A(2,3)Just remember that following mathematical conventions Octave indexes start with 1.

ans = 4

Almost everything in Octave is array (vector or matrix or similar) and index is no exception. Let's take a range for example. Range is a row vector with evenly spaced elements, e.g.:

octave:> 1:5Operation

ans =

1 2 3 4 5

octave:> A(1:3,1:5)

ans =

1 2 3 4 5

2 3 4 5 6

3 4 5 6 7

*returns whole A again because it selects all of its rows and columns. Ranges can exist by themselves as vectors but there is special type which is available only in the context of matrix index:*

**A(1:3, 1:5)**octave:> A(2:end,3:end)Ranges

ans =

4 5 6

5 6 7

*and*

**2:end***are defined only within concrete matrix context as keyword*

**3:end***indicates last row or column position within matrix. You can even select elements for last 2 rows and columns like this:*

**end**octave:> A(end-1:end,end-1:end)

ans =

5 6

6 7

### Logical Operations on Matrices

Logical arrays in Octave contain all logical elements and are usually results of relational operators with vectors and matrices like this:octave:> A != 3One cool application of this is inverting identity matrix:

ans =

1 1 0 1 1

1 0 1 1 1

0 1 1 1 1

octave:> I = eye(3)

I =

Diagonal Matrix

1 0 0

0 1 0

0 0 1

octave:> I == 0

ans =

0 1 1

1 0 1

1 1 0

### Euclidean Distance

Given two vectors (same size) find Euclidean distance between them.a = [0 0 0];If you find yourself writing in Octave more complex solutions for similar problems with vectors then stop and review your vectorization approach.

b = [1 2 2];

distance = sqrt(sumsq(a-b));

### Vectorizing indexes

Suppose you have a collection of*vectors in*

**m***dimensional space stored as*

**n-***x*

**m***matrix*

**n***. Suppose that the value of last (n-th) coordinate of these vectors is always 0 or 1. Then we want to produce 2 subsets of X - one subset of vectors with last coordinate equal to 0 and the other subset where vectors have last coordinate equal to 1:*

**X**n = size(X, 2);

X0 = X( find( X(:, n) == 0 ), :);

X1 = X( find( X(:, n) == 1 ), :);

### Randomness

Random numbers appear in many problems. Basic approach is a matrix filled with random numbers from uniform distribution on the interval (0, 1):octave:> rand(3,4)But Octave offers a few shortcuts. For one, such common distributions as normal, exponential, Poisson, and gamma each receive their own function

ans =

0.347937 0.317482 0.630678 0.245148

0.917634 0.649125 0.634592 0.837635

0.994745 0.092818 0.154936 0.966380

*,*

**randn***,*

**rande***, and*

**randp***.*

**randg**Function

*produces a row vector of randomly permuted integers from 1 to*

**randperm****:**

*n*octave:> randperm(5)If you are looking for an arbitrary vector with values between 0 and

ans =

3 1 4 5 2

*of size*

**N***then*

**n (n<=N)***gets id done (in this case*

**randperm***and*

**N = 100***):*

**n = 10**octave:> x = randperm(100)(1:10)

x =

88 1 89 25 76 19 78 38 99 34

This combined with vector indexing accomplishes rather elaborate task in short one-liner: suppose we have

*-dimensional vectors stored as*

**m n***x*

**m***matrix*

**n***and we need to pick*

**X***vectors (*

**k***) randomly. The following gets this done:*

**k < m**X(randperm(size(X, 1))(1:k), :);

Newer releases of Octave (I use 3.2.4) added functions

*and*

**randi***that offer even nice features.*

**randperm(n, m)**### Binary Singleton Expansion Function (bsxfun)

This function reminds me of Python*function, but having it in Octave is necessity (unlike in Python). When vectorizing in Octave we have a few options: 1) both parameters are of the same dimensions for element-wise application; 2) parameters are of compatible sizes for matrix operations like multiplication; 3) one parameter is a matrix and the other is a scalar.*

**map**And then we use

*for vectorizing everything else, for example applying a vector to each row:*

**bsxfun**X = rand(5, 3);

mu = mean(X);

sigma = std(X);

X_norm = bsxfun(@minus, X, mu);

X_norm = bsxfun(@divide, X_norm, sigma);

The operations above resulted in normalizing all 5 vectors (rows) from

*:*

**X***contains vectors with 0 means and standard deviations 1 for all 3 dimensions. In just 2 lines*

**X_norm***applied*

**bsxfun***(means) and*

**mu***(standard deviations) to each row of*

**sigma***and*

**X***.*

**X_norm**### Timing operations

Use functions*and*

**tic***to measure execution time in Octave to tune performance when necessary:*

**toc**octave:> tic; A * A'; toc

Elapsed time is 2.58e-008 seconds.

## 2 comments:

The use of randperm() to generate a number between 1 and N is brilliant. :)

Thanks. :)

Should be @rdivide, not @divide in the last example

Post a Comment