2015-04-01 171 views
-1

我試圖讓這個簡單的'for循環'工作。我不能變暗(F4)是一個6848x2矩陣。我只想分割兩個矩陣的行條目。這是我的...R循環和矩陣

   > dim(F3) 
      [1] 6848 2 

     > head(F3) 
    [,1]  [,2] 
     [1,] 140.9838 516.0239 
     [2,] 140.9838 516.0239 
     [3,] 140.9838 516.0239 
     [4,] 140.9838 516.0239 
     [5,] 140.9838 516.0239 
     [6,] 175.5093 515.2280 
     > dim(scale) 
     [1] 6848 1 

     F4 <- matrix(, nrow = nrow(F1), ncol = 1) 
     for (i in 1:t){ 
     F4[i,]<-(F3[i]/scale[i])} #ONLY WANT F3(i) ROW TO BE DIVIDED BY    SCALE(i) ROW 

    > dim(F4) #DOESN'T GIVE ME 6848x2 Matrix 
    [1] 6848 1 
+0

爲什麼要按元素劃分給你一個6848x2矩陣? – 2015-04-01 04:46:22

回答

1

這裏不需要使用for循環。這裏的矢量溶液:

F3/as.vector(sacle) ## BAD! use of built-in function "scale" as a variable! 

實施例:

mat <- matrix(1:8,4,2) 
sx <- matrix(1:4,4,1) 
mat /as.vector(sx) 

採用as.vector獲得-RID矩陣分割尺寸。

+0

agstudy,工作!謝謝!你能解釋爲什麼你做了1/*並用作as.vector(*)。 – 2015-04-01 05:44:02