I have data matrix (X) of the dimensions 5000x250 plus an extra parameter Y (Dim: 5000x1). The following loop gives me the desired results, but it takes forever to compute.
for (i in 1:ncol(X))
for (j in 1:nrow(X))
{
X[j,i]=Y[j,1]^X[j,i]
}
Is there any way to optimize this? If I didn't require the exponential, I'd use matrix multiplication. Thanks!
Turn your column vector y
into a matrix and use elementwise ^
.
matrix(y, nrow=nrow(X), ncol=ncol(X)) ^ X
or
rep(y, times=ncol(X)) ^ X