Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • from vector to matrix

    Hey,
    I want to transform the vector x into a Matrix A with 4 columns.
    I want to do that since I want to build a function, which sums up every fourthed element of the vector.I guess if I would create this matrix I could just use colsum(x) and than display col4.
    Any help would be great!

  • #2
    Hi Jay
    Two alternatives. The first handles if you only need every fourth value, the second is getting the matrix you asked for (but may not be needing):
    Code:
    : x = round(runiform(100,1) * 100)    // Generating a vector
    
    : select(x, !mod(1::rows(x), 4))    // Selecting every fourth value
             1
         +------+
       1 |  59  |
       2 |  10  |
       3 |  83  |
       4 |  53  |
       5 |  42  |
       6 |  52  |
       7 |   9  |
       8 |   4  |
       9 |  45  |
      10 |  71  |
      11 |  60  |
      12 |  75  |
      13 |  37  |
      14 |  42  |
      15 |  59  |
      16 |  75  |
      17 |  19  |
      18 |  78  |
      19 |  92  |
      20 |  63  |
      21 |  18  |
      22 |  17  |
      23 |  26  |
      24 |  92  |
      25 |  87  |
         +------+
    
    : colshape(x, 4)    // Creating the matrix you asked for
             1    2    3    4
         +---------------------+
       1 |  69   98   67   59  |
       2 |  80   78   65   10  |
       3 |  69   87   53   83  |
       4 |  93   17   55   53  |
       5 |  78   13   28   42  |
       6 |  14   33   47   52  |
       7 |   7    7   68    9  |
       8 |  13   87   25    4  |
       9 |  38   77   76   45  |
      10 |  41   30   68   71  |
      11 |  57   18   11   60  |
      12 |  63   63   99   75  |
      13 |  17   61   58   37  |
      14 |  30    1   67   42  |
      15 |  95    9   89   59  |
      16 |  40   67   42   75  |
      17 |  72   85   79   19  |
      18 |  39   24   34   78  |
      19 |  75   23   17   92  |
      20 |  31   90    8   63  |
      21 |  81   88    3   18  |
      22 |  58   41   62   17  |
      23 |  36   13    0   26  |
      24 |  65   93   82   92  |
      25 |  75   52   40   87  |
         +---------------------+
    
    : end
    Kind regards

    nhb

    Comment


    • #3
      Thanks a lot!
      Maybe one further question.
      Do you know how to sum these elements up?
      I`ve tried sum(select(x, !mod(1::rows(x), 4))) but that returns 0
      furthermore I´ve tried to store the selected result and than sum it up,
      x = (1, 3, 4, 4, 5, 6)
      y = select(x, !mod(1::rows(x), 4))
      sum(y)
      which did not work as well.
      Thanks again!

      Comment


      • #4
        Your x is a row vector. The function select needs a column vector, so you have to transpose.
        Also for a row vector you need to count columns (mistakes I also do all the time ).
        Look at the code:
        Code:
        : x = 1..50
        
        : y = select(x', !mod(1::cols(x), 4))
        
        : y
                 1
             +------+
           1 |   4  |
           2 |   8  |
           3 |  12  |
           4 |  16  |
           5 |  20  |
           6 |  24  |
           7 |  28  |
           8 |  32  |
           9 |  36  |
          10 |  40  |
          11 |  44  |
          12 |  48  |
             +------+
        
        : sum(y)
          312
        Now it works
        Kind regards

        nhb

        Comment


        • #5
          Nice! Thanks again!
          Is it possible to make it irrelevant wether its a volume or row vector?

          Comment


          • #6
            I don't think so. Some sort of consistency is required
            Kind regards

            nhb

            Comment

            Working...
            X