Storing matrices in a relational database

前端 未结 4 1150
予麋鹿
予麋鹿 2020-12-23 14:40

I am working on a project for a client and going through the initial database design. The project will be a simple web app for tracking processes and their outcomes within a

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-23 15:24

    Video memory, a very simple 2D matrix is stored as follows:

    ABCD
    EFGH
    IJKL
    

    in ram sequentially like an array as

    A,B,C,D,E,F,G,H,I,J,K,L

    element x,y can be found at array offset

    [y*width+x]
    

    for instance, x=2,y=2 (zero-based) refers to element K.

    [y*width+x]=[2*4+2]=10. array element 10 (again zero-based) = K, so you're good.

    Storing in a comma-delimited list will let you put a matrix of any size in an nvarchar field. This assumes that you don't need to query individual cells in SQL, but just grab the matrix as a whole and process it client-side.

    Your table may look like this:

    tbl_matrices
    ----
    id
    user_id
    matrix nvarchar(max)
    

提交回复
热议问题