- By
**tomai** - Follow User

- 133 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about ' Matrices' - tomai

**An Image/Link below is provided (as is) to download presentation**

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

### Matrices

or 2-Dimensional Arraysor Arrays of Arrays

1

2

3

4

0

1

2

3

4

A

B

C

D

E

A

B

C

D

E

0

0

F

G

C

I

J

F

G

H

I

J

1

1

K

L

M

N

O

2

2

K

L

M

N

O

Definition- In programming, a matrix is an array of arrays; that is, a collection of collection of elements. Generally, we envision this as a grid (Model 1), but is more accurately represented by a collection of collections (Model 2)

Model 1

Model 2

Places you may have heard of matrices

- The awesome dot-matrix printers of the 1980s printed images and text by putting either a dot or no dot in each “space” of the paper. They were horrible quality because if you looked closely it was usually pretty easy to make out the individual dots.

Image from Wikipedia

Places you may have heard of matrices

- Matrices are all over the place in mathematics, with several branches of the subject (such as linear algebra) making extensive use of them. We will be coding a mathematical matrix later on for a project, and perform operations such as addition and subtraction on them.
- In math, the number of columns in each row of a matrix must be the same; however, in Java arrays of arrays do not have this requirement.

Image from Wikipedia

Places you may have heard of matrices

- The use of the term “matrix” in the horrible 1999 movie The Matrixdoesn\'treally correspond to how we use the term in this class, although I guess maybe the green grid patterns of characters used to represent code in the movie could be stored in arrays of arrays.
- The movie\'s use of the term corresponds with the word\'s other definition, “something within or from which something else originates, develops, or takes form”

Declaring an array of arrays

- When we declare an array in Java, we give the type, then use the [] to show it is a collection of that type.int[] arr;
- When we declare an array of arrays, it works the same way. The type is an int[], so we have an int[][] (an array of int[]):

int[][] matrix;

- As with arrays, array of arrays can hold either primitive values (int[][]) or Objects (String[][])

Instantiating an array of arrays

- When we instantiate an array in Java, we have to give the number of elements that will be stored in the array. int[] arr = new int[SIZE_OF_ARRAY];
- Similarly, when we declare an array of arrays, we have to give the number of rows and number of columns the matrix will hold. Although how you visualize the data stored in the matrix is up to you, mathematics and programming conventions put the ROWS first, then the COLUMNS.

int[][] matrix = new int[NUM_ROWS][NUM_COLS];

Instantiating an array of arrays

- When we instantiate an array in Java, when can use the “shortcut method” to quickly fill up the array: int[] arr = { 3, 40, 2, 0 };
- Similarly, when we declare an array of arrays, we have to give the number of rows and number of columns the matrix will hold. Although how you visualize the data stored in the matrix is up to you, mathematics and programming conventions put the ROWS first, then the COLUMNS.

int[][] matrix ={ { 8, 1, 3}, { 3, 2, 5}, { 1, 9, 8} };

Accessing elements in an array of arrays

- We access an element in an array by giving the name of the array and brackets containing the index we want: int[] arr = new int[SIZE_OF_ARRAY];arr[0]=4; //first element of arr set to 4arr[arr.length-1]=4; //last element of arr set to 4
- Similarly, when we declare an array of arrays, we have to give the number of rows and number of columns the matrix will hold. Although how you visualize the data stored in the matrix is up to you, mathematics and programming conventions put the ROWS first, then the COLUMNS.

int[][] m = new int[NUM_ROWS][NUM_COLS];//top-left element of arr set to 4m[0][0] = 4;//bottom-right element of arr set to 4m[m.length-1][m[m.length-1].length - 1] = 4;

Accessing Length

- We access the length of an array with the .length property…no () arr.length
- With matrices, accessing nameOfMatrix.length gives you the number of rows, and nameOfMatrix[0].length gives you the number of columns in the first row.

matrix.length//number of rowsmatrix[0].length //number of columns in first row

matrix[matrix.length-1].length //number of columns in last row

Ragged (Non-Rectangular) Array of Arrays

- As mentioned before, in math matrices must be rectangular; all rows must have the same number of columns.
- In Java, you can have ragged array of arrays, where each row has a different length. I do not suggest doing this often, as it can get confusing, but it can be done.
- char[][] letters = { {\'A\', \'B\', \'C\'},

{\'D\', \'E\', \'F\', \'G\'},

{\'H\', \'I\' } };

- int[][] nums = new int[3][]; //3 rows, each of length not yet set

nums[0] = new int[4]; //first row set to length 4

nums[1] = new int[1]; //second row set to length 1

nums[2] = new int[10]; //third row set to length 10

Looping through Matrices

for(int r = 0; r<arr.length; r++)

{

for(int c = 0; c<arr[r].length; c++)

{

System.out.print(arr[r][c] + " ");

}

System.out.println(); //done with one row, go to next line

}

Looping through Matrices w/For-Each Loop

char[][] letters = //some already filled matrix of chars

for(char[] arr : letters)

{

System.out.println(Arrays.toString(arr));

}

Download Presentation

Connecting to Server..