Beruflich Dokumente
Kultur Dokumente
and Utilization
11-Matrices
Multidimensional Arrays
Instructor: Sridhar Iyer
IIT Bombay
IIT Bombay
IIT Bombay
CS 101 - 2012-2
Binary Search
Can we do faster than sequentially searching for y in
Array B?
IIT Bombay
O(log n)
CS 101 - 2012-2
Do Think-Pair-Share
IIT Bombay
CS 101 - 2012-2
[3] 100486
[4] 100638
[5] 100853
[6] 100965
[7] 101195
[8] 101273
[9] 101679
IIT Bombay
CS 101 - 2012-2
IIT Bombay
Multidimensional Arrays
We can use arrays with more than one dimension
int A[50][40];
IIT Bombay
CS 101 - 2012-2
0 Cell 0, Byte 1, 4
0
2, 8
3, 12
4, 16
1 5, 20
6, 24
7, 28
8, 32
9, 36
2 10, 40
11, 44
12, 48
13, 52
14, 5659
IIT Bombay
CS 101 - 2012-2
Matrix manipulations
2D arrays are commonly used to represent Matrices
and are found in programs such as:
Matrix multiplication
IIT Bombay
CS 101 - 2012-2
10
Basic operations
CS 101 - 2012-2
11
IIT Bombay
CS 101 - 2012-2
12
IIT Bombay
CS 101 - 2012-2
13
x0
y0
y1
=
a10 a11 a12
x1
x2
IIT Bombay
CS 101 - 2012-2
14
IIT Bombay
CS 101 - 2012-2
15
Matrix-matrix multiplication
float amat[lsize][msize], bmat[msize]
[nsize], cmat[lsize][nsize];
for (int crx=0; crx<lsize; ++crx) {
for (int ccx=0; ccx<nsize; ++ccx) {
cmat[crx][ccx] = 0;
for (int abx=0; abx<msize; ++abx) {
cmat[crx][ccx] +=
amat[crx][abx] * bmat[abx][ccx];
}
}
Like matrix-vector multiplication
}
But x has many columns
CS 101 - 2012-2
16
CS 101 - 2012-2
17
CS 101 - 2012-2
18
IIT Bombay
CS 101 - 2012-2
19
IIT Bombay
CS 101 - 2012-2
20
IIT Bombay
CS 101 - 2012-2
21