以下の重複while文(for文でも同じです)を高速化するにはどうしたらよろしいでしょうか?特に、重複while文中で解析した全結果を保存するために、fout <- file(filename, "w") で書き込むことをしており、apply系を適用する際にどのようにそこを書けばよいのか分からずに困っています。 宜しくお願いします。
time <- Sys.time() # 現在の日付
print(time)
r <- 0.0
step <- 1L*0.1 #rの自動増幅ステップ
Width <- 10
Height <- 10
MAXr <- (Width^2+Height^2)^0.5
while (r <= MAXr) { #rの変更(stepによる)
i <- 1
MAXi <- (Width*Height)/step
filename <- sprintf("~/R/Dr%03d.csv",as.integer(r)) #ファイル名をr名を使って連番にする。
fout <- file(filename, "w") # ファイルを書き込みモードで開く。
while (i <=MAXi){
Y <- ceiling(i/(Width)) #y座標
X <- round(i-(Y-1)*Width,1) #x座標
j <- 1
while(j <= MAXi){
y <- ceiling(j/(Width)) #y座標
x <- round(j-(y-1)*Width,1) #x座標
distance <- round(((x-X)^2+(y-Y)^2)^0.5,1) #小数点1桁まで if(round(distance,1) == round(r,1)) {Dr=1} else {Dr=0}
if((x==Width)&&(y==Height)) break
j <-j+1
writeLines(paste(Dr,X,Y,x,y,sep=","), fout)
}
if((X==Width)&&(Y==Height)) break
i <- i+1
} #close i
close(fout) #書き込みファイルを閉じる
Dr <- read.csv(filename, header=T, sep=",")
r <- r+step
} #r close
time2 <- Sys.time() # 現在の日付
print(time2)
proc.time <- time2-time
print(proc.time)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/12 23:13