[R] Large matrix into a vector

Marc Schwartz marc_schwartz at comcast.net
Wed Mar 28 19:39:11 CEST 2007


On Wed, 2007-03-28 at 10:27 -0700, A Ezhil wrote:
> Hi,
> 
> I have a matrix HR(9x27). I would like to make a
> single vector with elements: t(HR[,1]) followed by
> t(HR[,2]) and then t(HR[,3] ... etc. Is there any neat
> way of converting this matrix into a vector rather
> doing something like c(t(HR[,1]), t(HR[,2]), t(HR[,3])
> ..)?

Keep in mind that a matrix is simply a vector, with a 'dim' attribute.
In addition, the matrix elements are stored in column order, so:

  mat <- matrix(1:(9 * 27), ncol = 27)


> mat
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
 [1,]    1   10   19   28   37   46   55   64   73    82    91   100
 [2,]    2   11   20   29   38   47   56   65   74    83    92   101
 [3,]    3   12   21   30   39   48   57   66   75    84    93   102
 [4,]    4   13   22   31   40   49   58   67   76    85    94   103
 [5,]    5   14   23   32   41   50   59   68   77    86    95   104
 [6,]    6   15   24   33   42   51   60   69   78    87    96   105
 [7,]    7   16   25   34   43   52   61   70   79    88    97   106
 [8,]    8   17   26   35   44   53   62   71   80    89    98   107
 [9,]    9   18   27   36   45   54   63   72   81    90    99   108
      [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23]
 [1,]   109   118   127   136   145   154   163   172   181   190   199
 [2,]   110   119   128   137   146   155   164   173   182   191   200
 [3,]   111   120   129   138   147   156   165   174   183   192   201
 [4,]   112   121   130   139   148   157   166   175   184   193   202
 [5,]   113   122   131   140   149   158   167   176   185   194   203
 [6,]   114   123   132   141   150   159   168   177   186   195   204
 [7,]   115   124   133   142   151   160   169   178   187   196   205
 [8,]   116   125   134   143   152   161   170   179   188   197   206
 [9,]   117   126   135   144   153   162   171   180   189   198   207
      [,24] [,25] [,26] [,27]
 [1,]   208   217   226   235
 [2,]   209   218   227   236
 [3,]   210   219   228   237
 [4,]   211   220   229   238
 [5,]   212   221   230   239
 [6,]   213   222   231   240
 [7,]   214   223   232   241
 [8,]   215   224   233   242
 [9,]   216   225   234   243


> as.vector(mat)
  [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16
 [17]  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32
 [33]  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48
 [49]  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64
 [65]  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80
 [81]  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96
 [97]  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112
[113] 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
[129] 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
[145] 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
[161] 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
[177] 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
[193] 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
[209] 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
[225] 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
[241] 241 242 243


HTH,

Marc Schwartz



More information about the R-help mailing list