create a Vandermonde matrix in R - r

I am new to R and still learning it. I want to m-by-n Vandermonde matrix
I know it can be done via for loops to assign values to corresponding indices within the matrix, but it seems inefficient when m or n is large. I need some advices to have a simpler and efficient way to generate the Vandermonde matrix? Thanks for help in advance!

Use outer like this:
n <- 6; alpha <- 1:5 # test data
outer(alpha, seq(0, n-1), `^`)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 1 1 1 1 1
## [2,] 1 2 4 8 16 32
## [3,] 1 3 9 27 81 243
## [4,] 1 4 16 64 256 1024
## [5,] 1 5 25 125 625 3125

A base R solution is to define your custom function vander, where sapply + cumprod are used
vander <- function(alpha,n) t(sapply(alpha, function(k) c(1,cumprod(rep(k,n-1)))))
vm1 <- vander(alpha,n)
Another option is from package matrixcalc, in which vandermonde.matrix can make it
vm2 <- matrixcalc::vandermonde.matrix(alpha,n)
Example
Given alpha and n like below
alpha <- 1:4
n <- 5
then you will get
> vm1
[,1] [,2] [,3] [,4] [,5]
[1,] 1 1 1 1 1
[2,] 1 2 4 8 16
[3,] 1 3 9 27 81
[4,] 1 4 16 64 256
> vm2
[,1] [,2] [,3] [,4] [,5]
[1,] 1 1 1 1 1
[2,] 1 2 4 8 16
[3,] 1 3 9 27 81
[4,] 1 4 16 64 256

Related

How can I make the matrix as in the following image in R?

I know that I can make a simple matrix (for example 1->20 numbers, 4,5 rows) using this:
x<- array(1:20, dim=c(4,5)); x
But I have no idea how to make a matrix similar to the one on the picture...
matrix(rep(1:4,4)^rep(0:3,each=4), byrow=TRUE, nrow=4)
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 1 2 3 4
[3,] 1 4 9 16
[4,] 1 8 27 64
You can create a function if you want to be able to generate an analogous square matrix with an arbitrary number of rows.
power_mat = function(n) {
matrix(rep(1:n,n)^rep(1:n - 1, each=n), byrow=TRUE, nrow=n)
}
power_mat(6)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 1 1 1 1 1
[2,] 1 2 3 4 5 6
[3,] 1 4 9 16 25 36
[4,] 1 8 27 64 125 216
[5,] 1 16 81 256 625 1296
[6,] 1 32 243 1024 3125 7776
Solution
t(sapply(0:3, function(x) (1:4)^x))
Result
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 1 2 3 4
[3,] 1 4 9 16
[4,] 1 8 27 64
(You can easily verify this matches your matrix)
Explanation
Each row of the matrix is the vector c(1, 2, 3, 4) raised to the power of the row index (in a 0-based indexing system). So, we just use sapply() on the vector of powers to raise 1:4 to each power which will return a matrix. Because sapply() would return the results as column vectors, we use t() to transpose the result.
n <- 4
m <- 4
t(sapply( 0:(n-1), FUN = function (i){ return(c(1:m)**i) }))
where n is the number of rows, and m is the number of columns.
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 1 2 3 4
[3,] 1 4 9 16
[4,] 1 8 27 64
n = 4
m = rbind(rep(1, n), t(replicate(n-1, 1:n)))
m ^ (row(m) - 1)
# [,1] [,2] [,3] [,4]
#[1,] 1 1 1 1
#[2,] 1 2 3 4
#[3,] 1 4 9 16
#[4,] 1 8 27 64

R indexing arrays. How to index 3 dimensional array by using a matrix for the 3rd dimension

I'm having a question about indexing 3 dim arrays.
Say I have a 3 dimensional array
x<- c(1:36)
dim(x) <- c(3,4,3)
Now I want to extract values out of this array according to a matrix holding the 3rd dimension indices for all [i,j] positions.
y <- c(rep(1,4),rep(2,4),rep(3,4))
dim(y) <- c(3,4)
y
[,1] [,2] [,3] [,4]
[1,] 1 1 2 3
[2,] 1 2 2 3
[3,] 1 2 3 3
So the result should be giving this:
[,1] [,2] [,3] [,4]
[1,] 1 4 19 34
[2,] 2 17 20 35
[3,] 3 18 33 36
Is there some elegant way to do this? I know how to use two for loops to go over the array, but this is too slow for my data.
help("[") tells us this:
Matrices and arrays
[...]
A third form of indexing is via a numeric matrix with the one column for each dimension: each row of the
index matrix then selects a single element of the array, and the
result is a vector.
Thus, we transform your y matrix to a shape that conforms with this.
library(reshape2)
z <- x[as.matrix(melt(y))]
dim(z) <- dim(y)
# [,1] [,2] [,3] [,4]
#[1,] 1 4 19 34
#[2,] 2 17 20 35
#[3,] 3 18 33 36
I'm looking at this as an opportunity for some code golf. It's definitely possible to do this as a one-liner:
> `dim<-`(x[cbind(c(row(y)), c(col(y)), c(y))], dim(y))
[,1] [,2] [,3] [,4]
[1,] 1 4 19 34
[2,] 2 17 20 35
[3,] 3 18 33 36
As #Roland's answer shows, matrix/array indexing involves creating an n-column matrix and setting the columns equal to row, column, etc. position of each dimension of an n-dimensional array. We can use the row() and col() functions to extract the row and column positions of each element in y:
> row(y)
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 2 2 2 2
[3,] 3 3 3 3
> col(y)
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 1 2 3 4
[3,] 1 2 3 4
and y itself gives the third-dimension positions. wrapping each of those in c() turns them into a vector, so that they can be cbind-ed together to create an extraction matrix.
Then, there's just some fun use of dim<-() to fit it all on one line.

How to "customize" my elements in a matrix R

I wanna build an nxm matrix, n< m, and i want each element Mij = i^j
Any ideas? I tried multiplying various vectors etc but i never get the correct result.
n <- 3
m <- 4
outer(seq_len(n), seq_len(m), "^")
# [,1] [,2] [,3] [,4]
#[1,] 1 1 1 1
#[2,] 2 4 8 16
#[3,] 3 9 27 81
You can also use sapply:
t(sapply(1:n, `^`, 1:m))
# [,1] [,2] [,3] [,4]
#[1,] 1 1 1 1
#[2,] 2 4 8 16
#[3,] 3 9 27 81
Or even faster:
matrix(1:n, ncol=m, nrow=n)^matrix(1:m, ncol=m, nrow=n, byrow=T)
# [,1] [,2] [,3] [,4]
#[1,] 1 1 1 1
#[2,] 2 4 8 16
#[3,] 3 9 27 81

Flip the matrix

Hi everyone who loves while hates R:
Let's say you want to turn matrix M
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
to N
[,1] [,2] [,3]
[1,] 3 2 1
[2,] 6 5 4
[3,] 9 8 7
All you need to do is
N<-M[,c(3:1)]
And N's structure is still a matrix
However, when you want to turn matrix M
[,1] [,2] [,3]
[1,] 1 2 3
to N
[,1] [,2] [,3]
[1,] 3 2 1
if you do
N<-M[,c(3:1)]
R will give you
N
[1] 3 2 1
N now is a vector! Not a matrix!
My solution is
N<-M%*%diag(3)[,c(3:1)]
which needs big space to store the identity matrix however.
Any better idea?
You're looking for this:
N<-M[,c(3:1),drop = FALSE]
Read ?Extract for more information. This is also a FAQ. This behavior is one of the most common debates folks have about the way things "should" be in R. My general impression is that many people agree that drop = FALSE might be a more sensible default, but that behavior is so old that changing it would be enormously disruptive to vast swaths of existing code.
A=t(matrix(1:25,5,5))
B=matrix(0,5,5)
for(i in 1:5){
B[i,(nrow(A)+1-i)]=1
}
A
# [,1] [,2] [,3] [,4] [,5]
# [1,] 1 2 3 4 5
# [2,] 6 7 8 9 10
# [3,] 11 12 13 14 15
# [4,] 16 17 18 19 20
# [5,] 21 22 23 24 25
A%*%B
# [,1] [,2] [,3] [,4] [,5]
# [1,] 5 4 3 2 1
# [2,] 10 9 8 7 6
# [3,] 15 14 13 12 11
# [4,] 20 19 18 17 16
# [5,] 25 24 23 22 21

R. how to make matrix which have equal rows

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) )

Resources