Matrices Flashcards

1
Q

What is a matrix?

A

Two dimensional array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Can a single matrix hold elements of multiple data types?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the function to create a matrix?

A

matrix()

Ex: matrix(1:9, byrow = TRUE, nrow = 3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the arguments to matrix() aside from the collection of elements, and what do they do?

A

The argument byrow indicates whether the matrix is filled by the rows or the columns.

The argument nrow indicates the number of rows the matrix should have.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the function to name the rows and columns of a matrix?

A

rownames() and colnames().

Ex: rownames(my_matrix) <- c(“name1”, “name2”)

Ex: colnames(my_matrix) <- c(“name1”, “name2”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which functions calculate the totals for each row or column of a matrix?

A

rowSums() and colSums()

Ex: new_vector <- rowSums(my_matrix)

Ex: new_vector <- colSums(my_matrix)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which functions add columns or rows to a matrix, merging matrices and/or vectors together?

A

cbind() and rbind()

Ex: big_matrix <- cbind(matrix1, matrix2, vector1 …)

Ex: big_matrix <- rbind(matrix1, matrix2, vector1 …)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you select a single element from a matrix?

A

[num1, num2]

Ex: my_matrix[1,2]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can you select multiple in-sequence elements from a matrix?

A

[num1:num2, num3:num4]

Ex: my_matrix[1:3, 2:4]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How can you select an entire column or row from a matrix?

A

[,num] and [num,]

Ex: my_matrix[,1]

Ex: my_matrix[1,]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When selecting an element(s) from a matrix, is it written [rows, columns], or [columns, rows]?

A

[rows, columns]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What happens when a matrix is added, subtracted, etc. by a single object?

Ex: matrix(1:9, byrow = TRUE, nrow = 3) * 5

A

The result is calculated element-wise.

Ex Result: 5 10 15
20 25 30
35 40 45 is printed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What happens when two matrices are added, subtracted, etc.?

Ex: matrix(1:9, byrow = TRUE, nrow = 3) * matrix(1:9, byrow = TRUE, nrow = 3)

A

The result is calculated with each element being calculated with the element of the corresponding index in the other matrix.

Ex Result: 1 4 9
16 25 36
49 64 81 is printed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly