Martund
Martund

Reputation: 301

What does it mean to equate a matrix with a vector in R

In many posts here, people use operations like

X==x

where X is a matrix and x is a vector in R. The output is a matrix of TRUE and FALSE values of dimension same as that of X. How are TRUE and FALSE arranged in that matrix?

Upvotes: 3

Views: 50

Answers (1)

dww
dww

Reputation: 31452

A matrix is a vector with dimension attributes, hence the possibility to compare it to other vectors. For matrix operations, the matrix is treated as having values arranged by column. As we can see in the following

X = matrix(1:9, 3, 3)

X
#      [,1] [,2] [,3]
# [1,]    1    4    7
# [2,]    2    5    8
# [3,]    3    6    9

as.vector(X)
# [1] 1 2 3 4 5 6 7 8 9

We can see the dim attributes thus:

attributes(X)
# $dim
# [1] 3 3

If we remove the dim attributes, we are left with just a vector

attr(X, 'dim') = NULL
X
[1] 1 2 3 4 5 6 7 8 9

Upvotes: 2

Related Questions