2013-06-10 3 views
0

현재로서는 큰 행렬의 일부 기능을 계산하고 for-loop에서 모두 수행합니다. 예상대로 매우 느립니다. 나는 코드의 일부를 벡터화 할 수 있었지만 한 부분에 붙어있다.이 까다로운 행렬 계산 속도 향상

나는 조언/도움을 크게 주시면 감사하겠습니다! 2 기능이 벡터화 된 후

s1 <- MyMatrix #dim = c(5167,256) 
fr <- MyVector #vector of length 256 

tw <- 5 
fw <- 6 

# For each point S(t,f) we need the sub-matrix of points S_hat(i,j), 
# i in [t - tw, t + tw], j in [f - fw, f + fw] for the feature vector. 
# To avoid edge effects, I pad the original matrix with zeros, 
# resulting in a matrix of size nobs+2*tw x nfreqs+2*fw 
nobs <- dim(s1)[1] #note: this is 5167 
nf <- dim(s1)[2] #note: this is 256 
sp <- matrix(0, nobs+2*tw, nf+2*fw) 
t1 <- tw+1; tn <- nobs+tw 
f1 <- fw+1; fn <- nf+fw 
sp[t1:tn, f1:fn] <- s1 # embed the actual matrix into the padding 

nfeatures <- 1 + (2*tw+1)*(2*fw+1) + 1 
fsp <- array(NaN, c(dim(sp),nfeatures)) 
for (t in t1:tn){ 
    for (f in f1:fn){ 
    fsp[t,f,1] <- fr[(f - f1 + 1)] #this part I can vectorize 
    fsp[t,f,2:(nfeatures-1)] <- as.vector(sp[(t-tw):(t+tw),(f-fw):(f+fw)]) #this line is the problem 
    fsp[t,f,nfeatures] <- var(fsp[t,f,2:(nfeatures-1)]) 
    } 
} 

fspec[t1:tn, f1:fn, 1] <- t(matrix(rep(fr,(tn-t1+1)),ncol=(tn-t1+1))) 
#vectorized version of the first feature^

return(fsp[t1:tn, f1:fn, ]) #this is the returned matrix 
+0

이 보이는 'fsp [1 : t1,1 : f1,]'은 루프에서 사용되지 않습니다. 사물의 크기에 따라 이것은 중요 할 수 있습니다. 다음으로 'var (fsp [...])'계산을 벡터화하는 데 도움이되지 않을 것이라 확신합니까? –

답변

0

은 내가 VAR 기능은 벡터화하기 쉬운 것이라고 생각

모든 nfeatures``의 주어진 값, 즉 사용되지 않는 '층', 많이있는 것처럼