How to represent a 2-D data matrix in a database

后端 未结 6 1701
萌比男神i
萌比男神i 2020-12-07 11:38

I have a data set which consists of an ID and a matrix (n x n) of data related to that ID.

Both the column names (A,B,C,D) and the Row names (1,2,3) are also importa

相关标签:
6条回答
  • 2020-12-07 11:50

    Or even better what you can do is, create a logical array like structure. Say u want to store an m X n array.. Create m attributes in the table. In each attribute store n elements separated by delimiters ...

    while retrieving the data, simply do reverse parsing to easily get back the data..

    0 讨论(0)
  • 2020-12-07 11:53

    I'd probably implement it like this:

    Table MatrixData
    ----------------
    id
    rowName
    columnName
    datapoint
    

    If all you're looking for is storing the data, this structure will hold any size matrix and allow you to reconstitute any matrix from the ID. You will need some post-processing to present it in "matrix format", but that's what the front-end code is for.

    0 讨论(0)
  • 2020-12-07 11:54

    This is one of the reasons why PostgreSQL supports arrays as a data type. See

    • http://www.postgresql.org/docs/8.4/static/functions-array.html and
    • http://www.postgresql.org/docs/8.4/static/arrays.html

    Where it shows you can use syntax like ARRAY[[1,2,3],[4,5,6],[7,8,9]] to define the values of a 3x3 matrix or val integer[3][3] to declare a column type to be a 3x3 matrix.

    Of course this is not at all standard SQL and is PostgreSQL specific. Other databases may have similar-but-slightly-different implementations.

    0 讨论(0)
  • 2020-12-07 11:56

    RDBMSes aren't flat. The R part sees to that. What you need is:

    Table Entity
    ------------
    ID
    
    Table EntityData
    ----------------
    EntityID
    MatrixRow (1, 2, 3...)
    MatrixColumn (A, B, C, D...)
    Value
    

    Entity:EntityData is a one-to-many relationship; each cell in the matrix has an EntityData row.

    Now you have a schema that can be analyzed at the SQL level, instead of just being a data dump where you have to pull and extract everything at the application level in order to find out anything about it.

    0 讨论(0)
  • 2020-12-07 12:02

    can the data be thought of as "row data"? if so then maybe you could store each row as a Object (or XML Blob) with data A,B,C,D and then, in your "representation", you use something like a LinkedHashMap (assuming Java) to get the objects with an ID key.

    Also, it seems that by its very basic nature, a typical database table already does what you need doesn't it?

    0 讨论(0)
  • 2020-12-07 12:06

    If you want a truly relational solution:

    Matrix
    ------
    id
    
    Matrix_Cell
    -----------
    matrix_id
    row
    col
    value
    

    But constraints to make sure you had valid data would be hideous.

    I would consider a matrix as a single value as far as the DB is concerned and store it as csv:

    Matrix
    ------
    id
    cols
    data
    

    Which is somewhat lighter than XML.

    0 讨论(0)
提交回复
热议问题