我試圖更新一個全局變量(矩陣名: 「confusion.mat」):[R局部全局變量
confusion.mat <- structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(3L, 3L))
intersection.list <- c(1, 2)
然而,全局變量混亂的功能
register.hit <-function(categ){
confusion.mat[categ,categ] = confusion.mat[categ,categ] + 1
}
sapply(intersection.list,register.hit)
數據中.mat沒有更新(在調試模式下,我注意到函數register.hit中的局部變量confusion.mat已正確更新)。 任何幫助表示讚賞
你能製作一個可重現的例子嗎?我似乎無法在'R'中重新創建問題。 – Ruben
這是設計。 '功能'不應該有副作用。如果你的函數返回一些東西,可以使用'apply'函數。否則留一個'for'循環。當然,可能會有更好的向量化解決方案,但是你並沒有描述你實際想要達到的目標。 – Roland
也許這是你的目標? 'diag(confusion.mat)< - seq_len(nrow(confusion.mat))' – Roland