1
How to create an array in R, whose values are the result of a subtraction of values from a vector? ex.:
Vector
x <- c(a, b, c)
Matrix
1 a-a a-b a-c
2 b-a b-b b-c
3 c-a b-b c-c
1
How to create an array in R, whose values are the result of a subtraction of values from a vector? ex.:
Vector
x <- c(a, b, c)
Matrix
1 a-a a-b a-c
2 b-a b-b b-c
3 c-a b-b c-c
4
The function outer
was made to answer the question problem. By default it assumes the function "*"
to calculate the external product (Wikipedia Português, in English), but any other function may be passed.
a <- 1
b <- 3
c <- 6
x <- c(a, b, c)
outer(x, x, '-')
# [,1] [,2] [,3]
#[1,] 0 -2 -5
#[2,] 2 0 -3
#[3,] 5 3 0
1
Just think of the matrix as a set of vectors, then you form the rows and columns and join in a matrix.
Here already done at once, I create a vector with the concatenated lines and then transform into a matrix with 3 lines.
x <- c(1,2,3)
m <- matrix(c(x[1]-x, x[2]-x, x[3] - x), nrow = 3, byrow = T)
Browser other questions tagged r matrix
You are not signed in. Login or sign up in order to post.