Let's say, I have a vector of size 10. How do I create a matrix with position of vector elements arranged like this.
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 1
3 4 5 6 7 8 9 10 1 2
4 5 6 7 8 9 10 1 2 3
5 6 7 8 9 10 1 2 3 4
6 7 8 9 10 1 2 3 4 5
7 8 9 10 1 2 3 4 5 6
8 9 10 1 2 3 4 5 6 7
9 10 1 2 3 4 5 6 7 8
10 1 2 3 4 5 6 7 8 9
You could:
x = 1:10
matrix(x, nrow = length(x), ncol = length(x) + 1, byrow = T)[, -(length(x) + 1)]
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,] 1 2 3 4 5 6 7 8 9 10
# [2,] 2 3 4 5 6 7 8 9 10 1
# [3,] 3 4 5 6 7 8 9 10 1 2
# [4,] 4 5 6 7 8 9 10 1 2 3
# [5,] 5 6 7 8 9 10 1 2 3 4
# [6,] 6 7 8 9 10 1 2 3 4 5
# [7,] 7 8 9 10 1 2 3 4 5 6
# [8,] 8 9 10 1 2 3 4 5 6 7
# [9,] 9 10 1 2 3 4 5 6 7 8
#[10,] 10 1 2 3 4 5 6 7 8 9
As #flodel noted in the comments, you could, also, build the matrix with an extra row and remove it. And, also, use nicer format: head(matrix(x, nrow = length(x) + 1 , ncol = length(x)), -1).
How about this?
sapply(1:10, function(idx){
vec <- 1:10
if(idx != 1){
vec <- c(vec[idx:10], 1:(idx-1))
}
vec
}
)
Related
This is what I know in Matlab and want do same in r programming
A=[1 2 3;4 5 6; 7 8 9];
A =
1 2 3
4 5 6
7 8 9
[m,n]=size(A)
m=3
n=3
so here I have two distinct variables to which the dimension or size of 2D matrix is assigned automatically
> x<-c(1:10)
> x
[1] 1 2 3 4 5 6 7 8 9 10
> A=matrix(0,10,10)
> A=Toeplitz(x,c(x[1],rev(x[-1])))
> A
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 10 9 8 7 6 5 4 3 2
[2,] 2 1 10 9 8 7 6 5 4 3
[3,] 3 2 1 10 9 8 7 6 5 4
[4,] 4 3 2 1 10 9 8 7 6 5
[5,] 5 4 3 2 1 10 9 8 7 6
[6,] 6 5 4 3 2 1 10 9 8 7
[7,] 7 6 5 4 3 2 1 10 9 8
[8,] 8 7 6 5 4 3 2 1 10 9
[9,] 9 8 7 6 5 4 3 2 1 10
[10,] 10 9 8 7 6 5 4 3 2 1
> n=size(A)
> n
[1] 10 10
>[ m,n]=size(A)
this is not working so is there any way to assign the size of the 2Dmatrix to two distinct variable m and n in r.I am learning r programming and need help
I have a 6x6 matrix:
M = matrix(0,nrow = 6, ncol = 6);
for(i in 1:6)
for(j in 1:6)
M[i,j]<- i+j
> M
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 2 3 4 5 6 7
[2,] 3 4 5 6 7 8
[3,] 4 5 6 7 8 9
[4,] 5 6 7 8 9 10
[5,] 6 7 8 9 10 11
[6,] 7 8 9 10 11 12
I am using table() function to build a contingency table of the counts at each combination of factor levels.
table(M)
output:
> table(M)
M
2 3 4 5 6 7 8 9 10 11 12
1 2 3 4 5 6 5 4 3 2 1
Now I want to convert the table(M) to a data.frame using following:
pmfY <- data.frame(table(M))
But the pmfY$M changes to the follwong:
> pmfY$M
[1] 1 2 3 4 5 6 7 8 9 10 11
I am expecting pmfY$M to be the following vector in data frame.
2 3 4 5 6 7 8 9 10 11 12
Why pmfY$M is changed when converting table(M) to data frame and how to fix it?
I'm having a problem in creating the vector
1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9
with rep() and seq(). Could anyone be able to give me a hint?
We can use
rep(1:5, times = 5) + rep(0:4, each = 5)
# [1] 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9
or simpler (as R will recycle 1:5 automatically):
rep(0:4, each = 5) + 1:5
# [1] 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9
Is there a way to do that with the command seq?
1:5 is just seq(1,5), while 0:4 is seq(0,4).
matrix representation
For those who feel interested, we can also use outer:
as.numeric(outer(1:5, 0:4, "+"))
The initial call to outer generates this matrix:
# [,1] [,2] [,3] [,4] [,5]
#[1,] 1 2 3 4 5
#[2,] 2 3 4 5 6
#[3,] 3 4 5 6 7
#[4,] 4 5 6 7 8
#[5,] 5 6 7 8 9
then we use as.numeric to flatten it.
A better solution
Function sequence can now generate such sequence (it couldn't back in 2016):
sequence(rep(5, each = 5), 1:5)
# [1] 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9
Is there a function in R which switches the first element with the last one in a vector? I have a for loop which need that reordering. From:
months = seq(1:12)
[1] 1 2 3 4 5 6 7 8 9 10 11 12
I would like to have:
[1] 12 1 2 3 4 5 6 7 8 9 10 11
and then again:
[1] 11 12 1 2 3 4 5 6 7 8 9 10
...
until the 12th position.
If you need a matrix output
cbind(c(months),embed(c(months, months), 12)[-13,-12])
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
# [1,] 1 12 11 10 9 8 7 6 5 4 3 2
# [2,] 2 1 12 11 10 9 8 7 6 5 4 3
# [3,] 3 2 1 12 11 10 9 8 7 6 5 4
# [4,] 4 3 2 1 12 11 10 9 8 7 6 5
# [5,] 5 4 3 2 1 12 11 10 9 8 7 6
# [6,] 6 5 4 3 2 1 12 11 10 9 8 7
# [7,] 7 6 5 4 3 2 1 12 11 10 9 8
# [8,] 8 7 6 5 4 3 2 1 12 11 10 9
# [9,] 9 8 7 6 5 4 3 2 1 12 11 10
#[10,] 10 9 8 7 6 5 4 3 2 1 12 11
#[11,] 11 10 9 8 7 6 5 4 3 2 1 12
#[12,] 12 11 10 9 8 7 6 5 4 3 2 1
Or another approached suggested by #Marat Talipov
z <- length(months)
i <- rep(seq(z),z) + rep(seq(z),each=z) - 1
matrix(months[ifelse(i>z,i-z,i)],ncol=z)
I'm afraid that you have to come up with a home-made function, something like this one:
rotate <- function(v,i=1) {
i <- i %% length(v)
if (i==0) return(v)
v[c(seq(i+1,length(v)),seq(i))]
}
Couple of examples:
v <- seq(12)
rotate(v,1)
# [1] 2 3 4 5 6 7 8 9 10 11 12 1
rotate(v,-1)
# [1] 12 1 2 3 4 5 6 7 8 9 10 11
You can also use tail and head functions:
x = c(tail(x,n), head(x,-n))
and modify n to rotate n times
The permute package can do this for you:
ap <- allPerms(length(months),
control = how(within = Within(type = "series"),
observed = TRUE))
ap[rev(seq_len(nrow(ap))), ]
(because of the way allPerms() does its work, we need to reverse the order of the rows, which is what the last line does.)
This gives:
> ap[rev(seq_len(nrow(ap))), ]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 1 2 3 4 5 6 7 8 9 10 11 12
[2,] 12 1 2 3 4 5 6 7 8 9 10 11
[3,] 11 12 1 2 3 4 5 6 7 8 9 10
[4,] 10 11 12 1 2 3 4 5 6 7 8 9
[5,] 9 10 11 12 1 2 3 4 5 6 7 8
[6,] 8 9 10 11 12 1 2 3 4 5 6 7
[7,] 7 8 9 10 11 12 1 2 3 4 5 6
[8,] 6 7 8 9 10 11 12 1 2 3 4 5
[9,] 5 6 7 8 9 10 11 12 1 2 3 4
[10,] 4 5 6 7 8 9 10 11 12 1 2 3
[11,] 3 4 5 6 7 8 9 10 11 12 1 2
[12,] 2 3 4 5 6 7 8 9 10 11 12 1
Technically this only works because months is the vector 1:12 and allPerms() returns a permutation matrix of the indices of the thing you want permuted. For different inputs, use ap to index into the thing you want to permute
perms <- ap
perms[] <- months[ap[rev(seq_len(nrow(ap))), ]]
perms
Is there any function in R to find most frequently occuring element in matrix??I Have a matrix containing image pixels.I want to find which image pixel occur most frequently in the image matrix.I dont want to use the for loops since it would be very time taking to iterate over all the pixels of an image.
Set up some test data.
> (image = matrix(sample(1:10, 100, replace = TRUE), nrow = 10))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 4 4 2 7 2 2 3 8 2 5
[2,] 7 3 2 6 6 5 7 8 1 3
[3,] 7 5 7 9 4 9 4 8 2 7
[4,] 5 3 4 2 1 5 9 10 9 5
[5,] 9 10 7 2 7 4 9 1 1 9
[6,] 2 3 5 1 2 8 1 5 9 4
[7,] 5 4 10 5 9 10 1 6 1 10
[8,] 6 3 9 7 1 1 9 2 1 7
[9,] 5 9 4 8 9 9 5 10 5 4
[10,] 10 1 4 7 3 2 3 5 4 5
Do it manually.
> table(image)
image
1 2 3 4 5 6 7 8 9 10
12 12 8 12 15 4 11 5 14 7
Here we can see that the value 5 appeared most often (15 times). To get the same results programmatically:
> which.max(table(image))
5
5
Get mode (or majority value) in 1 line of code
using set.seed to generate same random matrix
> set.seed(123)
> image = matrix(sample(1:10, 100, replace = TRUE), nrow = 10)
> image
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 3 10 9 10 2 1 7 8 3 2
[2,] 8 5 7 10 5 5 1 7 7 7
[3,] 5 7 7 7 5 8 4 8 5 4
[4,] 9 6 10 8 4 2 3 1 8 7
[5,] 10 2 7 1 2 6 9 5 2 4
[6,] 1 9 8 5 2 3 5 3 5 2
[7,] 6 3 6 8 3 2 9 4 10 8
[8,] 9 1 6 3 5 8 9 7 9 1
[9,] 6 4 3 4 3 9 8 4 9 5
[10,] 5 10 2 3 9 4 5 2 2 6)
Mode value of matrix (if tie, its gives minimum tie value)
> names(which.max(table(image)))
[1] "5"
I do not know any function to do that directly but you can use these functions:
sort(table(as.vector(Matrix))