Community Server

The platform that enables you to build rich, interactive communities
Welcome to Community Server Sign in | Join | Help
in Search

Using the Write Command

Last post 01-16-2010, 11:34 PM by virgule. 9 replies.
Sort Posts: Previous Next
  •  02-18-2009, 5:03 PM 79

    Using the Write Command

    I have a vector of results (N=10,000) that I'd like to write to another file (to use in another program).

    I need this vector to be transposed so that the elements are in one column rather than in one row. How can I use the write command to do this? If I could write the transposed vector into excel or notepad that would be best.

    Thanks,

    Sherman

    Nevermind...it turns out it was really easy to get it to write it into notepad. Thanks!
  •  02-19-2009, 8:15 AM 80 in reply to 79

    Re: Using the Write Command

    As Sherman discovered, the WRITE command outputs its vectors as columns. For example, this command:
    WRITE window (1 2 3 4 5) (10 11 12)
    produces this output:
    1.0,10.0
    2.0,11.0
    3.0,12.0
    4.0,NaN
    5.0,NaN

    The WRITE command produces a csv (comma separated value) file by default. The "window" keyword forces WRITE to output to the output window instead of a file so you can preview the results. To write to a file, use the "file" keyword. The "NaN" means "Not a Number" and fills in the columns so they are all the same length. Of course the arguments to the WRITE command can be variables; I used literal vectors to clarify the example. For complete info on WRITE, see http://www.statistics101.net/userguide/UserGuide85.html.

    John
  •  10-07-2009, 5:33 AM 84 in reply to 80

    Re: Using the Write Command

    Random Walker:
    As Sherman discovered, the WRITE command outputs its vectors as columns.


    Hello. I have the opposite problem. I'm working with matrix-like data. I can work around the lack of matrix variable-type by modifying matrix-calculus to operate on a single very long array, but this is really not fun to display/output the resulting data (when I want to look at the raw data rather than statistics)

    Things get really complicated as my process has several variables, and I want to see how the variables affect the calculations, ie I want to compare the output of several runs of my program. A matrix could be used to store an output vector, incrementing by one row at each major run, but unfortunately there is no such function in Statistics101. If I could write/append to a file the output of each run, I'd build my matrix in a file, run after run. Unfortunately, the WRITE command lacks a TABLE argument.

    Any suggestions for writing:
    - one output vector to a file in a single ascii row?
    - several output vectors to a file in a table form, as in PRINT TABLE?

    Thanks for the fantastic program, it is really brilliant!
    Gus
  •  10-07-2009, 7:37 AM 85 in reply to 84

    Re: Using the Write Command

    Gus,

    Have you looked at the Matrix subroutines that are included in the Statistics101 subroutine library? They are in the file lib/matrix_commands.txt. Read the comments at the top of the file, the descriptions of each subroutine, and the examples at the end of the file.

    The matrix subroutines are also listed by name in the edit window popup menu under "(Help) Commands and Subroutines by Category" in the "Matrix Math" category.

    There is a MATRIX_PRINT subroutine that might do what you want. Here's a quick example using MATRIX_PRINT:
    INCLUDE "lib\matrixCommands.txt"
    COPY 1,5 6,10 11,15 matrixA      'create a 3x5 matrix
    COPY 5 Acolumns               'number of columns in matrix.
    PRINT "MatrixA:"
    MATRIX_PRINT matrixA Acolumns
    Here's the output from the above:
    MatrixA:
    (1         2         3         4         5         )
    (6         7         8         9         10        )
    (11        12        13        14        15        )
    Hope that helps. Let me know if I missed your point.

    John
  •  10-07-2009, 9:26 AM 86 in reply to 85

    Re: Using the Write Command

    Thanks, you could have said RTFM ;-)

    Actually, I'm 1/2 way through, in Section 2 "solving problems". I had reached the conclusion after section 1 that the only data type available was the 1-D vector. It's a different way of thinking but it doesn't prevent calculus - and the code doesn't become ugly either.

    If I can do straight matrix code, then it's going to become really interesting!

    Many thanks for the friendly support and extra-fast response time!

    Gus
  •  10-07-2009, 7:36 PM 87 in reply to 86

    Re: Using the Write Command

    Hum, my apologies for a poorly phrased explanation of my issue. I am mixing up two separate issues:
    - Matrix calculus (found it, thank you very much, it works as you stated and works fine)
    - Writing to a file, one vector in one row (my original reply/question to the first post).

    I have several real world population data sets of between 1,000 and 50,000 values. I'm trying to simulate the evolution of these populations over 1,000 iterations of a process with some variables (to see the impact of the variables). Eventually I'll do some stats on the results, but first I need to be certain I am simulating correctly. My objective is to plot graphically the 1,000 iterations of one population vector, to visually check that my process variables are reasonable (3D plot).

    I cannot use STS101 to create the graphic plot (obvious limitations)

    I cannot WRITE the population vector at each iteration, as I would end up with an extremely long, one-column file, not  usable for a graphic plot.

    I cannot store intermediate results in a set of vector variables to be written later in one pass (as per WRITE syntax), as naming the 1,000 temporary variables becomes a headache.

    Matrix calculus doesn't help here, it's purely an I/O question.

    I've run out of ideas and need help to create the equivalent of PRINT FILE (one vector on one line, as in PRINT command, but written to a file rather than output window).

    Please?
    Gus
  •  10-08-2009, 8:45 AM 88 in reply to 87

    Re: Using the Write Command

    Gus,

    You're right. The WRITE command doesn't do what you want. I will add a keyword option "ROWS" to the command so it will write vectors in rows instead of columns. I expect it will take a few days to do this and make sure it works right, and then a while longer to update the documentation. But if you're in a hurry, I can get you the working code as soon as it's ready ahead of the documentation. I think the ROWS and APPEND options together will do what you want, i.e., write each vector horizontally to a single line in the file in successive iterations of your program.

    By the way, I found an error in the MATRIX_TRANSPOSE subroutine. Here is a corrected version you can paste into the lib/matrixCommands.txt file in place of the original in your existing Statistics101 installation. (It will be corrected in the next upload of the program to the website.)

    'Computes the transpose of matrixVec.
    'Inputs:
    '   matrixVec: The matrix to be transposed.
    '   numCols: The number of columns in the matrix.
    'Outputs:
    '   outMatrix: The transposed matrix.
    '   outCols: The number of columns in the transposed matrix. This
    '      will be needed if you use this matrix in another subroutine.
    '
    NEWCMD MATRIX_TRANSPOSE matrixVec numCols outMatrix outCols @"matrix math" ?"Computes the transpose of matrixVec"
       LET numRows = SIZE(matrixVec) / numCols
       LET outCols = numRows
       LET outRows = numCols
       FOREACH row 1,outRows
          FOREACH col 1,outCols
             LET newPosition =  (col - 1) * numCols + row
             SCORE newPosition positions
          END
       END
       TAKE matrixVec positions outMatrix
    END
    Regards,

    John
  •  10-08-2009, 6:48 PM 89 in reply to 88

    Re: Using the Write Command

    John,

    No, I'm not in a hurry, I'm just a little guy experimenting with some weird ideas. I'll wait while you work on the priority stuff first.

    But I'd be happy to test your code before you update the documentation ;-)

    Thanks a million
    Gus


  •  10-18-2009, 12:44 PM 90 in reply to 89

    Re: Using the Write Command

    I just finished uploading the latest version of Statistics101 to the website. It is version 1.4.3. I added the ROWS keyword to both WRITE and READ. I also added a new feature, the Subroutine Browser, which you can access from the menu Window->Show Subroutine Browser. The corrected version of the TRANSPOSE subroutine is also included.

    Check it out!

    John
  •  01-16-2010, 11:34 PM 91 in reply to 90

    Re: Using the Write Command

    I just realized I never said thank you nor closed this thread.
    Thank you!
    The new function works as requested, and works well.

    For those interested to pursue in this direction (exporting large data set for plotting in external apps), my painful experience has been as follows:
    - if plotting on a rectangular, regular grid, it doesn't really matter if printing in rows or columns. Most decent plotting software can either transpose the data matrix or swap coordinates ;-)
    - if plotting on a non-rectangular and/or non-regular grid (eg with holes in the grid), no "normal" software can read any kind of matrix output, whether in rows or columns, and display things **correctly**. The ONLY "normal" way to read & manipulate plot data in this case, is to write one row per data point, with all the point coordinates on that row (ie you should NOT use the new function described in this thread :-(.

    Gus
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems