I have a matrix:
ex:
> x
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
When i access eighth element it gives me 7 like
> x[8]
7
I want to access 8 when i type x[8] like
> x[8]
8
The fact is R indexes a matrix elements in top left to bottom left format but i want to index it in top left to to top right format.
How is this possible? Are there any additional arguments to use to do so?
Try this
t(x)[index]
You input is
> x = t(matrix(1:12, 4))
> x
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
You can get
> t(x)[1]
[1] 1
> t(x)[8]
[1] 8
> t(x)[12]
[1] 12
Related
I am trying to construct a function f that can rotate a matrix in a clock-wise manner.
The function should looks like this
f <- function(mat, k) {...}
where mat is the matrix, k is the times we perform 90-degree clock-wise rotation. Let's say, k=1 means we rotate the matrix 90 degrees, k=2 means 180 degrees, and so on.
If k > 1, I want to save all those rotated matrices (for i = 1,2,....,k) into a list, such that I can track how the rotation evolves.
Thanks in advance!
I guess you can use rot90 from package pracma to implement the fundamental rotation functionality.
The code below use a recursion method to save all those rotated matrices through 0 to k times of clock-wise rotations
library(pracma)
f <- function(mat, k) {
if (k==0) return(list(mat))
u <- f(mat,(k-1)%%4)
c(u,list(rot90(tail(u,1)[[1]])))
}
Example
Given a matrix mat like below
mat <- matrix(1:12,nrow = 4)
we will see
> f(mat,4)
[[1]]
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
[[2]]
[,1] [,2] [,3] [,4]
[1,] 9 10 11 12
[2,] 5 6 7 8
[3,] 1 2 3 4
[[3]]
[,1] [,2] [,3]
[1,] 12 8 4
[2,] 11 7 3
[3,] 10 6 2
[4,] 9 5 1
[[4]]
[,1] [,2] [,3] [,4]
[1,] 4 3 2 1
[2,] 8 7 6 5
[3,] 12 11 10 9
[[5]]
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
Since the first element of list is the original matrix mat, you can remove it via
f(mat,k)[-1]
Can matrix row and column names be set to defaults (e.g., [1,], [2,]... [,1], [,2]...) in R?
For example, is there a quick way to transform a matrix like this
x1 <- matrix(1:9,nrow=3,ncol=3,dimnames=list(1:3,letters[1:3]))
> x1
a b c
1 1 4 7
2 2 5 8
3 3 6 9
into this
> x1
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
You're looking for dimnames<-:
dimnames(x1) <- NULL
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
You can see the help file by typing ?dimnames. It is also linked from ?matrix.
Let's say I have a simple vector
v <- 1:5
I can add the vector to each element within the vector with the following code to generate the resulting matrix.
matrix(rep(v, 5), nrow=5, byrow=T) + matrix(rep(v, 5), nrow=5)
[,1] [,2] [,3] [,4] [,5]
[1,] 2 3 4 5 6
[2,] 3 4 5 6 7
[3,] 4 5 6 7 8
[4,] 5 6 7 8 9
[5,] 6 7 8 9 10
But this seems verbose and inefficient. Is there a more concise way to accomplish this? Perhaps some linear algebra concept that is evading me?
outer should do what you want
outer(v, v, `+`)
# [,1] [,2] [,3] [,4] [,5]
# [1,] 2 3 4 5 6
# [2,] 3 4 5 6 7
# [3,] 4 5 6 7 8
# [4,] 5 6 7 8 9
# [5,] 6 7 8 9 10
Posting this answer not for up votes but to highlight Franks comment. You can use
sapply(v,"+",v)
I have a matrix called m as follows
> m<-matrix(1:15,3,5)
> m
[,1] [,2] [,3] [,4] [,5]
[1,] 1 4 7 10 13
[2,] 2 5 8 11 14
[3,] 3 6 9 12 15
I want to remove the first column of this matrix. Within a function I pass a value called j, which is always 1 less than the number of columns in m (In this example j is 4).
Therefore I used the following code
>m[,2:4+1]
[,1] [,2] [,3]
[1,] 7 10 13
[2,] 8 11 14
[3,] 9 12 15
But it is giving only the last 3 columns. Then I changed the code as follows
>m[,2:(4+1)]
This time I had the correct output.
Also it is giving the same output for following code as well
> m[,1:4+1]
Somebody please explain me how the following codes work?
>m[,2:4+1]
>m[,1:4+1]
: has higher precedence than +, therefore 2:4+1 gets interpreted at (2:4)+1 which is the same as 3:5:
2:4+1
[1] 3 4 5
Similarly, 1:4+1 gets interpreted as 2:5:
1:4+1
[1] 2 3 4 5
To remove columns in a matrix, its probably easier to use the negative subscript input to [:
m[,-1]
[,1] [,2] [,3] [,4]
[1,] 4 7 10 13
[2,] 5 8 11 14
[3,] 6 9 12 15
How do I subset a matrix to get a vector?
> (m <- matrix(1:15,nrow=5,ncol=3))
[,1] [,2] [,3]
[1,] 1 6 11
[2,] 2 7 12
[3,] 3 8 13
[4,] 4 9 14
[5,] 5 10 15
> (v <- c(1,3))
[1] 1 3
> (u <- c(2,4))
[1] 2 4
what I want is the vector:
> c(m[2,1],m[4,3])
[1] 2 14
but what I get is a matrix:
> m[u,v]
[,1] [,2]
[1,] 2 12
[2,] 4 14
I guess I can use diag but I would rather do it in one step.
Just make sure you are passing a matrix of indices:
> m[cbind(u,v)]
[1] 2 14
For a single point, transpose gives you a matrix:
> m[t(c(4,3))]
[1] 14
> m[t(c(2,1))]
[1] 2