Rows and Columns

You create a row matrix consisting of the numbers 4 5 6 7 by entering the numbers inside []-brackets and separating the numbers by a comma or a blank.

>>> r1=[4, 5, 6, 7]

r1 =

4 5 6 7

>>> r2=[4 5 6 7]

r2 =

4 5 6 7

You create a column matrix consisting of the numbers 0.1 0.2 0.3 by entering the numbers inside []-brackets and separating the numbers by a semicolon.

>>> c1=[0.1; 0.2; 0.3]

c1 =

0.10000
0.20000
0.30000

>>> c2=[0.4; 0.5]

c2 =

0.40000
0.50000

Row and column matrices are sometimes called vectors.

You can combine row matrices or column matrices

>>> r=[r1, r2]

r =

4 5 6 4 5 6

>>> c=[c1; c2]

c =

0.10000
0.20000
0.30000
0.40000
0.50000

>>> try_a_mix=[r1, c1]

>>>error: number of rows must match (3 != 1) near line 43, column 16

The first number in a vector has index 1. You can get the number at a specified index.

>>> r(2)
ans = 5
>>> c(5)
ans = 0.50000
>>> r(7)
error: A(I): Index exceeds matrix dimension.

You can use the same notation that creates numbers in for-statements for creating vectors.

>>> r3=3:7
r3 =

3 4 5 6 7

>>> r4=1:3:10
r4 =

1 4 7 10

You can use the same notation for extracting a vector from a vector.

>>> format bank;
>>> r5=0.1:0.1:1
r5 =

0.10 0.20 0.30 0.40 0.50 0.60 0.70 0.80 0.90 1.00

>>> subvector1=r5(2:4)
subvector1 =

0.20 0.30 0.40

>>> subvector2=r5(1:2:10)
subvector2 =

0.10 0.30 0.50 0.70 0.90

You can turn a row into a column or the other way around by doing a transpose using the operator '.

>>> r=[1, 2, 3]
r =

1.00 2.00 3.00

>>> c=r'
c =

1.00
2.00
3.00

by Malin Christersson under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 Sweden License