Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multidimensional Arrays

Similar presentations


Presentation on theme: "Multidimensional Arrays"— Presentation transcript:

1 Multidimensional Arrays

2 Two Dimensional Arrays
Table of data for each experiment and outcome. Table of grades for each student and assignments. Table of grayscale values for each pixel in a 2D image. Mathematical abstraction. Matrix. Java abstraction. 2D array. "Microarrays allow us to see if a particular gene is on or off in a particular tissue. The colors on this picture correspond to whether or not the gene is expressed. So each row is a gene, and each column is a particular experiment, for example a particular type of tissue. If you see a red spot for some gene for breast tumors, that means this gene is expressed in breast cancer. But that same gene is not expressed in the brain, for example." Gene 1 Gene n gene expressed Reference: Botstein & Brown group gene not expressed

3 Two Dimensional Arrays in Java
Array access. Use a[i][j] to access element in row i and column j. Zero-based indexing. Row and column indices start at 0. double[][] a = new double[10][3]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 3; j++) { a[i][j] = 0.0; } Declaring and Initializing a 10-by-3 Array

4 Setting 2D Array Values at Compile Time
Initialize 2D array by listing values. double[][] p = { { .02, .92, .02, .02, .02 }, { .02, .02, .32, .32, .32 }, { .02, .02, .02, .92, .02 }, { .92, .02, .02, .02, .02 }, { .47, .02, .47, .02, .02 }, };

5 Memory Layout 2-D Arrays
Row

6 Row lengths can be non-uniform Can use .length
Ragged Arrays Row lengths can be non-uniform Can use .length int[][] a = ...; for (int rows=0; rows < a.length; rows++) { for (int cols=0; cols < a[rows].length; cols++) System.out.print(" " + a[rows][cols]); System.out.println(""); } Number of rows Number of columns for a given row

7 Matrix Addition Matrix addition. Given two N-by-N matrices a and b, define c to be the N-by-N matrix where c[i][j] is the sum a[i][j] + b[i][j]. double[][] c = new double[N][N]; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) c[i][j] = a[i][j] + b[i][j];

8 Matrix Multiplication
Matrix multiplication. Given two N-by-N matrices a and b, define c to be the N-by-N matrix where c[i][j] is the dot product of the ith row of a and the jth column of b. all values initialized to 0 double[][] c = new double[N][N]; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) // dot-product of row i and column j for (int k = 0; k < N; k++) c[i][j] += a[i][k] * b[k][j];

9 Self-Avoiding Random Walk

10 Self-Avoiding Random Walk
Model. N-by-N lattice. Start in the middle. Randomly move to a neighboring intersection, avoiding all previous intersections. Applications. Polymers, statistical mechanics, etc. A1. Not hard to convince yourself that you will always escape in a 5-by-5 grid

11 Made up of long chains of “monomers”
Example: Polymers Made up of long chains of “monomers” DNA: 108 nucleotides Excluded Volume: No two monomers can occupy the same place Polymer chains cannot self-intersect Form “coils” Polymer chains can be modeled as random walks DNA

12 Self-Avoiding Random Walk
Model. N-by-N lattice. Start in the middle. Randomly move to a neighboring intersection, avoiding all previous intersections. Applications. Polymers, statistical mechanics, etc. Q. What fraction of time will you escape the lattice? Q. What is the average path length? A1. Not hard to convince yourself that you will always escape in a 5-by-5 grid

13 Simulating Self-Avoiding Random Walks
Represent the 2-D grid as a 2-D array Array element[x][y] denotes whether the given intersection has been visited (boolean array) Initialize all the elements of the array to false Set element [x][y] to true when you visit that intersection

14 Self-Avoiding Walk: Implementation Dr. Java Demo
public class SelfAvoidingWalk { public static void main(String[] args) { int N = Integer.parseInt(args[0]); // lattice size int T = Integer.parseInt(args[1]); // number of trials int deadEnds = 0; // trials resulting in dead end for (int t = 0; t < T; t++) { boolean[][] a = new boolean[N][N]; // intersections visited int x = N/2, y = N/2; // current position - center while (x > 0 && x < N-1 && y > 0 && y < N-1) { if (a[x-1][y] && a[x+1][y] && a[x][y-1] && a[x][y+1]) { deadEnds++; break; } a[x][y] = true; // mark as visited double r = Math.random(); if (r < 0.25 && !a[x+1][y]) x++; else if (r < 0.50 && !a[x-1][y]) x--; else if (r < 0.75 && !a[x][y+1]) y++; else if (r < 1.00 && !a[x][y-1]) y--; System.out.println(100*deadEnds/T + "% dead ends");

15 Self-Avoiding Random Walks in a 21-by-21 Grid

16 Summary Arrays. Organized way to store huge quantities of data.
Almost as easy to use as primitive types. Can directly access an element given its index.


Download ppt "Multidimensional Arrays"

Similar presentations


Ads by Google