3

Matrices and Vectors

3.1 Overview

Both MATLAB and R can work directly with vectors and with m × n matrices (with the

usual convention that m speciﬁes the number of rows and n the number of co lumns). Both

platforms support vectorized computations, i.e., expressions that implicitly operate on entire

vectors or matrices. However, there a re some important diﬀerences between the two.

• A key diﬀerence in syntax is that R uses square brackets to delimit matrix s ubscripts

(e.g., A[2,3]), while MATLAB uses parentheses (e.g., A(2,3)). R’s syntax has the ad-

vantage here because when reading MATLAB code, it is not immediately apparent

whether g(2,3) is accessing an element of a matrix g, or calling a function g with two

parameters.

• In R, a vector is a primitive data type , and a matrix (which is actually a special case of

an array) is another distinct type. A vector with n elements is neither a row vector nor

a column vector, but simply an ordered set of n values. Care must sometimes b e taken

to ensure that a value is stored as a vecto r or matrix in particular .

In MATLAB, the fundamental data type is a matrix. A scalar value is simply a 1 × 1

matrix, while a vector with n elements is either an n × 1 matrix (column vector) or a

1 × n matrix (row vector). Care must sometimes be taken to ensure that a vector is a

row vector or a column vector in particular.

• In R, you can use array subscripting on expressions. For example, to a c cess the element

in row 1, column 2 of the matrix A

2

, you can do ( A%*%A)[1,2]. You cannot perform

the corresp onding command (A*A)(1,2) in MATLAB. The easiest way around this is

to use a temporary variable, e.g., tmp=A*A; tmp(1, 2), although there is another way

(see Sec tion 3.9).

3.2 Creating vectors

In R, a vector is an ordered s e quence of values; it is neither a row vector nor a column

vector. In MATLAB, a vector is simply a special ca se of a matrix which has either one row

or one column.

Below a re many of the common ways of creating vectors.

1. Create a vector containing the spe ciﬁed values 4, 8, 15, 16, 23, 42

19

20 R and MATLAB

R MATLAB

c(4,8,15,16,23,42)

Or you can call sca n(), then enter the

values separated by spaces, and pres s

Return/Enter twice to signal the end of

the values. To explicitly construct a row

vector (s tored as a matrix), use

cbind(4,8,15,16,23,42); for a column

vector, use rbind(4,8,15,16,23,42).

Either [4,8,15,16,23,42] or [4 8 15

16 23 42] for a row vector. For a column

vector, [4;8;15;16;23;42] or

[4

8

15

16

23

42]

(R): The often-used function c “combines” its arguments; it can work with many data

types.

(MATLAB): When entering matrices, co mmas or spaces can be used as delimiters be-

tween columns. Semicolons or newlines can be used between rows.

2. Vector of length k containing all zeros

rep(0,k) or numeric(k) zeros(k,1) (for column vector) or

zeros(1,k) (for row vector)

3. Vector of length k containing the value j in all positions

rep(j,k) j*ones(k,1) or repmat(j,k,1) (for

column vector); j*ones(1,k) or

repmat(j,1,k) (for row vector)

(R): rep is a widely used function which replicates its argument. It c an work with various

data types, and can be used in a few diﬀerent ways with vectors.

(MATLAB): repmat is used to replicate a matrix; because a scalar is simply a 1 × 1

matrix, it can also be use d as above.

4. Sequence with unit increment: 7, 8, 9, 10, 11

7:11 7:11 (for row vector), or (7:11)' (for

column vector).

(MATLAB): The apostrophe tr ansposes a matrix (but see entries 66 and 67 ), and can

also b e used to produce column vectors instead of row vectors for the entries below.

5. Values j − 1 through k + 1 w ith unit increment

(j-1):(k+1) j-1:k+1

The fact that the colon has higher order of precedence than arithmetic operators in R

but not in MATLAB is a major nuisance. I have seen many people struggle to ﬁnd why

their code is broken, only for it to be caused by this issue. In R, writing j-1:k+1 r eally

means j-(1:k)+1 which is almost never what was intended. To be extra-cautious, I suggest

always using parentheses when wr iting expressions like this in either platform.

6. Sequence with increment -1: 11, 10, 9, 8, 7

11:7 11:-1:7

If you have gotten used to doing things like 11:7 in R, be careful when using MATLAB,

because it produces an empty matrix, and does not display any warnings or explicit signs

of trouble.

7. Sequence with speciﬁed increment: 4, 7, 10, 13

seq(4,13,by=3) or simply seq(4 ,13,3) 4:3:13

Get *R and MATLAB* now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.