Let the matrices A and B.
A=c(1:5)*matrix(1,5,5)
B=10*A
that is,
> A
[,1] [,2] [,3] [,4] [,5]
[1,] 1 1 1 1 1
[2,] 2 2 2 2 2
[3,] 3 3 3 3 3
[4,] 4 4 4 4 4
[5,] 5 5 5 5 5
> B
[,1] [,2] [,3] [,4] [,5]
[1,] 10 10 10 10 10
[2,] 20 20 20 20 20
[3,] 30 30 30 30 30
[4,] 40 40 40 40 40
[5,] 50 50 50 50 50
I would like, for example, to switch the first rows between the matrices A and B, that is
> A
[,1] [,2] [,3] [,4] [,5]
[1,] 10 10 10 10 10
[2,] 2 2 2 2 2
[3,] 3 3 3 3 3
[4,] 4 4 4 4 4
[5,] 5 5 5 5 5
> B
[,1] [,2] [,3] [,4] [,5]
[1,] 1 1 1 1 1
[2,] 20 20 20 20 20
[3,] 30 30 30 30 30
[4,] 40 40 40 40 40
[5,] 50 50 50 50 50
using a function, and without using any indermediate vector or a for loop.
Update
As per you update in the comment, you can try
lapply(
1:nrow(B),
function(k) {
setNames(
Map(
function(x, ind,r) {
x[ind, ] <- r
x
},
list(A, B),
list(1,k),
list(B[k, ], A[1, ])
), c("A", "B")
)
}
)
which gives
[[1]]
[[1]]$A
[,1] [,2] [,3] [,4] [,5]
[1,] 10 10 10 10 10
[2,] 2 2 2 2 2
[3,] 3 3 3 3 3
[4,] 4 4 4 4 4
[5,] 5 5 5 5 5
[[1]]$B
[,1] [,2] [,3] [,4] [,5]
[1,] 1 1 1 1 1
[2,] 20 20 20 20 20
[3,] 30 30 30 30 30
[4,] 40 40 40 40 40
[5,] 50 50 50 50 50
[[2]]
[[2]]$A
[,1] [,2] [,3] [,4] [,5]
[1,] 20 20 20 20 20
[2,] 2 2 2 2 2
[3,] 3 3 3 3 3
[4,] 4 4 4 4 4
[5,] 5 5 5 5 5
[[2]]$B
[,1] [,2] [,3] [,4] [,5]
[1,] 10 10 10 10 10
[2,] 1 1 1 1 1
[3,] 30 30 30 30 30
[4,] 40 40 40 40 40
[5,] 50 50 50 50 50
[[3]]
[[3]]$A
[,1] [,2] [,3] [,4] [,5]
[1,] 30 30 30 30 30
[2,] 2 2 2 2 2
[3,] 3 3 3 3 3
[4,] 4 4 4 4 4
[5,] 5 5 5 5 5
[[3]]$B
[,1] [,2] [,3] [,4] [,5]
[1,] 10 10 10 10 10
[2,] 20 20 20 20 20
[3,] 1 1 1 1 1
[4,] 40 40 40 40 40
[5,] 50 50 50 50 50
[[4]]
[[4]]$A
[,1] [,2] [,3] [,4] [,5]
[1,] 40 40 40 40 40
[2,] 2 2 2 2 2
[3,] 3 3 3 3 3
[4,] 4 4 4 4 4
[5,] 5 5 5 5 5
[[4]]$B
[,1] [,2] [,3] [,4] [,5]
[1,] 10 10 10 10 10
[2,] 20 20 20 20 20
[3,] 30 30 30 30 30
[4,] 1 1 1 1 1
[5,] 50 50 50 50 50
[[5]]
[[5]]$A
[,1] [,2] [,3] [,4] [,5]
[1,] 50 50 50 50 50
[2,] 2 2 2 2 2
[3,] 3 3 3 3 3
[4,] 4 4 4 4 4
[5,] 5 5 5 5 5
[[5]]$B
[,1] [,2] [,3] [,4] [,5]
[1,] 10 10 10 10 10
[2,] 20 20 20 20 20
[3,] 30 30 30 30 30
[4,] 40 40 40 40 40
[5,] 1 1 1 1 1
You can try the code below
list2env(
setNames(
Map(
function(x, r) {
x[1, ] <- r
x
},
list(A, B),
list(B[1, ], A[1, ])
), c("A", "B")
),
envir = .GlobalEnv
)
replace seems to work without any intermediate
replace(A, cbind(1, 1:ncol(A)), B[1,])
replace(B, cbind(1, 1:ncol(A)), A[1,])
Note that once we do the assignment to the original object, the second assignment is not possible as the original object is changed
A clean way to swap is to create a temporary object and rm it
tmp <- A[1,]
A[1, ] <- B[1, ]
B[1, ] <- tmp
rm(tmp)
gc()
Or probably create a function, and do the swap inside the function, thus the activation record is deleted once it exit the function (as these are pass by value)
f1 <- function(a, b) {
t1 <- a[1,]
a[1,] <- b[1,]
b[1,] <- t1
return(list(a, b))
}
list2env(setNames(f1(A, B), c('A', 'B')), .GlobalEnv)
Related
Good afternoon!
Assume we have a vector and a matrix :
v = c(2,3,4)
[1] 2 3 4
m=matrix(1:9,ncol=3)
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
I'm searching an efficient way ( or built-in function ) to get three matrices m1 , m2 , m3 such that :
m1=v[1]*m
m2=v[2]*m
m3=v[3]*m
We could obtain this using a 3d-array :
my_fct<-function(m,v){
f=array(data=rep(NA,nrow(m)*ncol(m)*length(v)),dim = c(nrow(m),ncol(m),length(v)))
for (j in c(1:length(v))){
f[,,j]=v[j]*m
}
return(f)
}
my_fct(m,v)
, , 1
[,1] [,2] [,3]
[1,] 2 8 14
[2,] 4 10 16
[3,] 6 12 18
, , 2
[,1] [,2] [,3]
[1,] 3 12 21
[2,] 6 15 24
[3,] 9 18 27
, , 3
[,1] [,2] [,3]
[1,] 4 16 28
[2,] 8 20 32
[3,] 12 24 36
I hope my request is clear!
Thank you a lot for help !
As 'v' is a vector and we want each element to be multiplied by the same matrix 'm', an option is to loop over the element of 'v' and do the multiplication
lapply(v, `*`, m)
-output
[[1]]
[,1] [,2] [,3]
[1,] 2 8 14
[2,] 4 10 16
[3,] 6 12 18
[[2]]
[,1] [,2] [,3]
[1,] 3 12 21
[2,] 6 15 24
[3,] 9 18 27
[[3]]
[,1] [,2] [,3]
[1,] 4 16 28
[2,] 8 20 32
[3,] 12 24 36
Another base R option
> Map(`*`, list(m), v)
[[1]]
[,1] [,2] [,3]
[1,] 2 8 14
[2,] 4 10 16
[3,] 6 12 18
[[2]]
[,1] [,2] [,3]
[1,] 3 12 21
[2,] 6 15 24
[3,] 9 18 27
[[3]]
[,1] [,2] [,3]
[1,] 4 16 28
[2,] 8 20 32
[3,] 12 24 36
Here I have a
x <- array(1:20, dim=c(4,5))
x
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20
I also have a index array
i <- array(c(1:3,3:1), dim=c(3,2))
[,1] [,2]
[1,] 1 3
[2,] 2 2
[3,] 3 1
Then
x[i]
will extract X[1,3], X[2,2] and X[3,1]
However, what if I have
i <- array(c(1:3,3:1), dim=c(2,3))
The output is
x[i]
[1] 1 2 3 3 2 1
How can I understand this result?
For, example I if a had matrix like this:
realmatrix=matrix(1:16,ncol=4,nrow=4)
Which would give this:
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
And I would like to make a function that would replace the two columns with a certain value ( for example 1:4) if it has a even number of colums, then the result should be something like this:
[,1] [,2] [,3] [,4]
[1,] 1 1 1 13
[2,] 2 2 2 14
[3,] 3 3 3 15
[4,] 4 4 4 16
And if the matrix has odd numbers of colums, the function should replace only the odd column, that is to say the central one:
This is the matrix with odd numbers of columns:
realmatrix2=matrix(1:12,ncol=3,nrow=4)
The final result:
[,1] [,2] [,3]
[1,] 1 1 9
[2,] 2 2 10
[3,] 3 3 11
[4,] 4 4 12
Thanks a lot!
Try:
fun2 <- function(mat, val){
stopifnot(length(val)==nrow(mat))
n <- ncol(mat)
if( (n/2) %%2 ==0){
mat[, c(n/2, n/2+1)] <- val
}
else {
mat[, ceiling(n/2)] <- val
}
mat
}
fun2(realmatrix, 1:4)
# [,1] [,2] [,3] [,4]
#[1,] 1 1 1 13
#[2,] 2 2 2 14
#[3,] 3 3 3 15
#[4,] 4 4 4 16
fun2(realmatrix2,1:4)
# [,1] [,2] [,3]
#[1,] 1 1 9
#[2,] 2 2 10
#[3,] 3 3 11
#[4,] 4 4 12
realmatrix5=matrix(1:32, ncol=8,nrow=4)
fun2(realmatrix5, 1:4)
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
#[1,] 1 5 9 1 1 21 25 29
#[2,] 2 6 10 2 2 22 26 30
#[3,] 3 7 11 3 3 23 27 31
#[4,] 4 8 12 4 4 24 28 32
Update
If you want to change the rows:
funR <- function(mat, val){
stopifnot(length(val)==ncol(mat))
n <- nrow(mat)
if((n/2) %%2==0){
mat[c(n/2, n/2+1),] <- rep(val, each=2)
}
else {
mat[ceiling(n/2),] <- val
}
mat
}
funR(realmatrix, 1:4)
# [,1] [,2] [,3] [,4]
#[1,] 1 5 9 13
#[2,] 1 2 3 4
#[3,] 1 2 3 4
#[4,] 4 8 12 16
realmatrix3 <- matrix(1:15, ncol=5)
funR(realmatrix3, 1:5)
# [,1] [,2] [,3] [,4] [,5]
#[1,] 1 4 7 10 13
#[2,] 1 2 3 4 5
#[3,] 3 6 9 12 15
I have a matrix:
R> pippo.m
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
[4,] 13 14 15 16
[5,] 17 18 19 20
[6,] 21 22 23 24
and I would like to transform this matrix in a 3D array with dim=(2,4,3). Passing through the transponse of pippo.m I am able to obtain a similar result but with columns and rows rotated.
> pippo.t <- t(pippo.m)
> pippo.vec <- as.vector(pippo.t)
> pippo.arr <- array(pippo.vec,dim=c(4,2,3),dimnames=NULL)
> pippo.arr
, , 1
[,1] [,2]
[1,] 1 5
[2,] 2 6
[3,] 3 7
[4,] 4 8
, , 2
[,1] [,2]
[1,] 9 13
[2,] 10 14
[3,] 11 15
[4,] 12 16
, , 3
[,1] [,2]
[1,] 17 21
[2,] 18 22
[3,] 19 23
[4,] 20 24
Actually, I would prefer to mantain the same distribution of the original data, as rows and colums represent longitude and latitude and the third dimension is time. So I would like to obtain something like this:
pippo.a
, , 1
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
, , 2
[,1] [,2] [,3] [,4]
[1,] 9 10 11 12
[2,] 13 14 15 16
, , 3
[,1] [,2] [,3] [,4]
[1,] 17 18 19 20
[2,] 21 22 23 24
How can I do?
Behold the magic of aperm!
m <- matrix(1:24,6,4,byrow = TRUE)
> aperm(array(t(m),c(4,2,3)),c(2,1,3))
, , 1
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
, , 2
[,1] [,2] [,3] [,4]
[1,] 9 10 11 12
[2,] 13 14 15 16
, , 3
[,1] [,2] [,3] [,4]
[1,] 17 18 19 20
[2,] 21 22 23 24
I have row f. I want create matrix R such that every row of it is equal f.
What is the most efficient way to do it in R?
with a row
f=c(1,22,33,44,55,66)
get its length
lf=length(f)
Then make the matrix
R=matrix(rep(f,lf),
ncol=lf,
byrow=T)
Gives:
R
[,1] [,2] [,3] [,4] [,5]
[1,] 1 33 44 55 66
[2,] 1 33 44 55 66
[3,] 1 33 44 55 66
[4,] 1 33 44 55 66
[5,] 1 33 44 55 66
R <- matrix(f, 1)[rep(1,n), ]
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 1 2 3 4 5
[3,] 1 2 3 4 5
[4,] 1 2 3 4 5
[5,] 1 2 3 4 5
Or even more compact:
R <- rbind(f)[rep(1,n), ]
[,1] [,2] [,3] [,4] [,5]
f 1 2 3 4 5
f 1 2 3 4 5
f 1 2 3 4 5
f 1 2 3 4 5
f 1 2 3 4 5
Note that rownames of matrices do not need to be unique, unlike the case with data.frames.
Here is one possibility:
mymat <- do.call( rbind, rep(list(f), 10) )