Lecture11_The_Array

Tasks:
The following tasks are directly copied from the published script, please complete the tasks.

Contents

clear all
close all

Create a row vector

A=[1 2 3 4] % different elements can be split by a 'space' or a 'comma'.
B=[1,2,3,4]
A =

1 2 3 4


B =

1 2 3 4

Create a column vector

A=[1;2;3;4]
A =

1
2
3
4

Convert a row vector to a column vector

A=[1 2 3 4 5]
B=A'
A =

1 2 3 4 5


B =

1
2
3
4
5

Create a row array of evenly spaced numbers

A=0:2:10 % first:increment:maximum
A=0:10 % first:maximum
A =

0 2 4 6 8 10


A =

0 1 2 3 4 5 6 7 8 9 10

linspace command

linspace(0,10,6) % linspace(start,last,number of values)
linspace(0,1,4)
linspace(0,10)
ans =

0 2 4 6 8 10


ans =

0 0.3333 0.6667 1.0000


ans =

Columns 1 through 7

0 0.1010 0.2020 0.3030 0.4040 0.5051 0.6061

Columns 8 through 14

0.7071 0.8081 0.9091 1.0101 1.1111 1.2121 1.3131

Columns 15 through 21

1.4141 1.5152 1.6162 1.7172 1.8182 1.9192 2.0202

Columns 22 through 28

2.1212 2.2222 2.3232 2.4242 2.5253 2.6263 2.7273

Columns 29 through 35

2.8283 2.9293 3.0303 3.1313 3.2323 3.3333 3.4343

Columns 36 through 42

3.5354 3.6364 3.7374 3.8384 3.9394 4.0404 4.1414

Columns 43 through 49

4.2424 4.3434 4.4444 4.5455 4.6465 4.7475 4.8485

Columns 50 through 56

4.9495 5.0505 5.1515 5.2525 5.3535 5.4545 5.5556

Columns 57 through 63

5.6566 5.7576 5.8586 5.9596 6.0606 6.1616 6.2626

Columns 64 through 70

6.3636 6.4646 6.5657 6.6667 6.7677 6.8687 6.9697

Columns 71 through 77

7.0707 7.1717 7.2727 7.3737 7.4747 7.5758 7.6768

Columns 78 through 84

7.7778 7.8788 7.9798 8.0808 8.1818 8.2828 8.3838

Columns 85 through 91

8.4848 8.5859 8.6869 8.7879 8.8889 8.9899 9.0909

Columns 92 through 98

9.1919 9.2929 9.3939 9.4949 9.5960 9.6970 9.7980

Columns 99 through 100

9.8990 10.0000

Create a Matrix

b=[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16] % the semi-colon will perform as a 'line-break'.
b =

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Special array creation

zeros(2,3) % 2 rows by 3 columns of 'zeros'
ones(2,3) % 2 rows by 3 columns of 'ones'
ans =

0 0 0
0 0 0


ans =

1 1 1
1 1 1

Make a matrix filled with a particular number, multiply ones(m,n) by that number

A=100*ones(3,4)
A =

100 100 100 100
100 100 100 100
100 100 100 100

Combine two vectors

r=[2 4 20]
w=[9 -6 3]
combined=[r w]
combined_2=[r;w]
r =

2 4 20


w =

9 -6 3


combined =

2 4 20 9 -6 3


combined_2 =

2 4 20
9 -6 3

Addressing

A=[4 5 6 7; 8 9 10 11; 1 2 3 4; 2 3 4 5]
A(2,4) % the element of 'row 2 column 4'.
A =

4 5 6 7
8 9 10 11
1 2 3 4
2 3 4 5


ans =

11


Colon Operator and Addressing
: (Super Important)

v(2:5) selects elements 2 through 5 that is v(2), v(3), v(4), v(5).

v(:) represents all the row or column elements of the vector v.
A(:,3) denotes all the row elements in the third column of the matrix A.
A(3,:) denotes all the column elements in the third row of the matrix A.
A(:,2:5) denotes all the row elements in the second through fifth columns of A.
A(2:3,1:3) denotes all the elements in the second and third rows that are also in the first through third columns.

Examples:







Change the elements in an array

A=[1 2 3;4 5 6]
A(1,3)=1000000
A =

1 2 3
4 5 6


A =

1 2 1000000
4 5 6

Assigning values with an 'out-of-range' index will grow the array; This may or may not be what you want

A=[1 2 3;4 5 6;7 8 9]
A(4,2)=1000000
A =

1 2 3
4 5 6
7 8 9


A =

1 2 3
4 5 6
7 8 9
0 1000000 0

Scalars work for subarray replacement – they just scale up to the right size

A=[1 2 3;4 5 6;7 8 9]
A([2 3],[2 3])=7
A =

1 2 3
4 5 6
7 8 9


A =

1 2 3
4 7 7
7 7 7


Send eveything and HW8 in one 'html file and send it to homeworkflc02@gmail.com by Monday.