Lecture 4
Array addressing
Eng. Mohamed Awni
Electrical & Computer Engineering
Dept.
Agenda
2
 Vector addressing (linear address)
 Matrix addressing (Subscripts address).
 Colon (:) Operator in addressing arrays.
 Adding elements to existing arrays.
 Deleting elements of an existing arrays.
 Exercise & Homework
Vector Addressing (Linear address)
is its position in the arrayAddress of an element in an array
>> ve = [35 46 78 23 5 14 81 3 55]
ve =
35 46 78 23 5 14 81 3 55
1 2 3 4 5 6 7 8 9 position or index of element
ve(1) = 35
ve(2)= 46
.
.
.
.
ve(9)=55
Subscript Address • The address of an element in a matrix is
denoted by the row index and the column
index
• An element in the row number; i, and the
column number; j, of the matrix p is p (i, j)
• The address of an element in the first row
and the first column of the matrix is p(1,1)
not p(0, 0).
• The matrix indices begin from 1 (not 0 (as in C))
• The matrix indices must be positive integer
Matrix addressing (Subscript address)
4 10 1 6 2
8 1.2 9 4 25
7.2 5 7 1 11
0 0.5 4 5 56
23 83 13 0 10
1
2
Rows (m) 3
4
5
Columns
(n)
1 2 3 4 5
1 6 11 16 21
2 7 12 17 22
3 8 13 18 23
4 9 14 19 24
5 10 15 20 25
A = A (2,4)
A (17)
Matrix addressing
Colon (:) Operator in addressing arrays
Can be used to address a range of elements in a
vector or a matrix
A Colon (:)
Addressing a range of vector elements
7
4 10 1 6 2
8 1.2 9 4 25
7.2 5 7 1 11
0 0.5 4 5 56
23 83 13 0 10
1
2
3
4
5
1 2 3 4 5
1 6 11 16 21
2 7 12 17 22
3 8 13 18 23
4 9 14 19 24
5 10 15 20 25
A =
A(1:5,5)
A(:,5)
A(21:25)
A(4:5,1:2)
A([4 9;5 10])
Addressing a range of matrix elements
Colon (:) Operator in addressing arrays
A(:,n) Elements in all the rows of column “n” of the matrix A. (column)
A(n,:) Elements in all the columns of row “n” of the matrix A (row)
A(:,m:n) Elements in all the rows between columns m and n of matrix A
A(:,2:3) A(2,:)
A([2 7 12 17 22])
A(m:n,p:q) Elements in rows m through n and columns p through q of the
matrix A
Adding elements to existing arrays.
• An existing vector can be changed to have more elements
• An existing matrix can be changed to have more rows and/or columns
1 Adding elements to a vector
A = 98 23 12 11 231 2 3 4 5
>> A(6)=12;
>> A
A = 98 23 12 11 23 6
12
240
7 8
A = 98 23 12 11 23 6
12
>> A(8)=24;
>> A
2 Appending vector y to vector x
>> z= [x,y]
>> z= [x ; y]
z = 98 23 12 11 23 11 24 15 16 17
1 2 3 4 5 6 7 8 9 10
y = 11 24 15 16 17
1 2 3 4 5
x = 98 23 12 11 231 2 3 4 5
Z =
98 23 12 11 231 3 5 7 9
11 24 15 16 17
2 4 6 8 10
Adding elements to existing arrays.
3 Adding elements to a matrix Adding new rows and/ or columns
A =
98 23 12 111 3 5 7
11 24 15 16
2 4 6 8
>>A(3,:)=4:4:16
A =
98 23 12 111 4 7 10
11 24 15 16
2 5 8 11
4 8 12 16
3 6 9 12
>>A(:,5)= 4:2:6
A =
98 23 12 111 3 5 7 9
11 24 15 16
2 4 6 8 10
4
6
Adding new row
Adding new column
Adding elements to existing arrays.
Appending a new matrix to the existing one
>> C = [B A] >> B(5,5)= 333
Adding elements to existing arrays.
Deleting elements of an existing arrays
Exercise
Homework

Lecture4

  • 1.
    Lecture 4 Array addressing Eng.Mohamed Awni Electrical & Computer Engineering Dept.
  • 2.
    Agenda 2  Vector addressing(linear address)  Matrix addressing (Subscripts address).  Colon (:) Operator in addressing arrays.  Adding elements to existing arrays.  Deleting elements of an existing arrays.  Exercise & Homework
  • 3.
    Vector Addressing (Linearaddress) is its position in the arrayAddress of an element in an array >> ve = [35 46 78 23 5 14 81 3 55] ve = 35 46 78 23 5 14 81 3 55 1 2 3 4 5 6 7 8 9 position or index of element ve(1) = 35 ve(2)= 46 . . . . ve(9)=55
  • 4.
    Subscript Address •The address of an element in a matrix is denoted by the row index and the column index • An element in the row number; i, and the column number; j, of the matrix p is p (i, j) • The address of an element in the first row and the first column of the matrix is p(1,1) not p(0, 0). • The matrix indices begin from 1 (not 0 (as in C)) • The matrix indices must be positive integer Matrix addressing (Subscript address)
  • 5.
    4 10 16 2 8 1.2 9 4 25 7.2 5 7 1 11 0 0.5 4 5 56 23 83 13 0 10 1 2 Rows (m) 3 4 5 Columns (n) 1 2 3 4 5 1 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 19 24 5 10 15 20 25 A = A (2,4) A (17) Matrix addressing
  • 6.
    Colon (:) Operatorin addressing arrays Can be used to address a range of elements in a vector or a matrix A Colon (:) Addressing a range of vector elements
  • 7.
    7 4 10 16 2 8 1.2 9 4 25 7.2 5 7 1 11 0 0.5 4 5 56 23 83 13 0 10 1 2 3 4 5 1 2 3 4 5 1 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 19 24 5 10 15 20 25 A = A(1:5,5) A(:,5) A(21:25) A(4:5,1:2) A([4 9;5 10]) Addressing a range of matrix elements Colon (:) Operator in addressing arrays A(:,n) Elements in all the rows of column “n” of the matrix A. (column) A(n,:) Elements in all the columns of row “n” of the matrix A (row) A(:,m:n) Elements in all the rows between columns m and n of matrix A A(:,2:3) A(2,:) A([2 7 12 17 22]) A(m:n,p:q) Elements in rows m through n and columns p through q of the matrix A
  • 8.
    Adding elements toexisting arrays. • An existing vector can be changed to have more elements • An existing matrix can be changed to have more rows and/or columns 1 Adding elements to a vector A = 98 23 12 11 231 2 3 4 5 >> A(6)=12; >> A A = 98 23 12 11 23 6 12 240 7 8 A = 98 23 12 11 23 6 12 >> A(8)=24; >> A
  • 9.
    2 Appending vectory to vector x >> z= [x,y] >> z= [x ; y] z = 98 23 12 11 23 11 24 15 16 17 1 2 3 4 5 6 7 8 9 10 y = 11 24 15 16 17 1 2 3 4 5 x = 98 23 12 11 231 2 3 4 5 Z = 98 23 12 11 231 3 5 7 9 11 24 15 16 17 2 4 6 8 10 Adding elements to existing arrays.
  • 10.
    3 Adding elementsto a matrix Adding new rows and/ or columns A = 98 23 12 111 3 5 7 11 24 15 16 2 4 6 8 >>A(3,:)=4:4:16 A = 98 23 12 111 4 7 10 11 24 15 16 2 5 8 11 4 8 12 16 3 6 9 12 >>A(:,5)= 4:2:6 A = 98 23 12 111 3 5 7 9 11 24 15 16 2 4 6 8 10 4 6 Adding new row Adding new column Adding elements to existing arrays.
  • 11.
    Appending a newmatrix to the existing one >> C = [B A] >> B(5,5)= 333 Adding elements to existing arrays.
  • 12.
    Deleting elements ofan existing arrays
  • 13.
  • 14.