Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #49273

Reshape a matrix

From "WJ" <w_a_x_man@yahoo.com>
Newsgroups comp.lang.forth
Subject Reshape a matrix
Date 2016-07-21 08:55 +0000
Organization A noiseless patient Spider
Message-ID <nmq2mf$mvp$1@dont-email.me> (permalink)

Show all headers | View raw


R:

  Create a vector:

  > v = seq(2, len=20, by=3)
  > v
   [1]  2  5  8 11 14 17 20 23 26 29 32 35 38 41 44 47 50 53 56 59

  Convert it to a matrix:

  > dim(v) = c(4,5)
  > v
       [,1] [,2] [,3] [,4] [,5]
  [1,]    2   14   26   38   50
  [2,]    5   17   29   41   53
  [3,]    8   20   32   44   56
  [4,]   11   23   35   47   59

  Reshape it:

  > dim(v) = c(5,4)
  > v
       [,1] [,2] [,3] [,4]
  [1,]    2   17   32   47
  [2,]    5   20   35   50
  [3,]    8   23   38   53
  [4,]   11   26   41   56
  [5,]   14   29   44   59

  Make a copy that has a different shape:

  > matrix(v, nrow=2)
       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
  [1,]    2    8   14   20   26   32   38   44   50    56
  [2,]    5   11   17   23   29   35   41   47   53    59

  Make a copy that is transposed, i.e., make the columns
  the rows:

  > t(v)
       [,1] [,2] [,3] [,4] [,5]
  [1,]    2    5    8   11   14
  [2,]   17   20   23   26   29
  [3,]   32   35   38   41   44
  [4,]   47   50   53   56   59

  Add the matrix to itself:

  > v+v
       [,1] [,2] [,3] [,4]
  [1,]    4   34   64   94
  [2,]   10   40   70  100
  [3,]   16   46   76  106
  [4,]   22   52   82  112
  [5,]   28   58   88  118

  Add 5000 to each element:

  > 5000+v
       [,1] [,2] [,3] [,4]
  [1,] 5002 5017 5032 5047
  [2,] 5005 5020 5035 5050
  [3,] 5008 5023 5038 5053
  [4,] 5011 5026 5041 5056
  [5,] 5014 5029 5044 5059

  Is it easier in Forth?

-- 
Jewish drug dealers, child porn pushers, and slave traders are free
from prosecution in Israel.  Israel does not consider these to be
crimes ... so long as the victims are non-Jews.
www.theoccidentalobserver.net/2009/08/the-culture-of-deceit/

Back to comp.lang.forth | Previous | Next | Find similar | Unroll thread


Thread

Reshape a matrix "WJ" <w_a_x_man@yahoo.com> - 2016-07-21 08:55 +0000

csiph-web