AN_
AN_

Reputation: 143

Storing a matrix in django model

I would like to store a matrix (from a file) in django and upload it to sqlite. I have seen that some people advise to store it per cells, but as it is a huge matrix (more than a thousand elements) I wouldn't like to store it that way, but as a whole.
I am thinking about storing it as a list, but it is also not easy, because the elements are floats.
Is there a simple/doable way to store my matrix?

view.py

new_file = [[8.2, 14.2, -6.1],
           [12.6,7.0,4.7], 
           [-1.1,3.6,21.1]]

def file_list(request):
    content = new_file[:]
    content = chain.from_iterable(zip(*content))
    extract_data = Cell(content = content)
    extract_data.save()

model.py

class Cell(models.Model):
    content = models.FloatField() #ok, obviously this doesn't work

Upvotes: 0

Views: 190

Answers (1)

Pulkit Kumar Agarwal
Pulkit Kumar Agarwal

Reputation: 86

If your priority to save your memory, you can try storing your matrix as a large string. As float need 8 byte memory and a char needs only 1 byte memory.

For example:

Your list is

new_file = [[8.2, 14.2, -6.1],
           [12.6,7.0,4.7], 
           [-1.1,3.6,21.1]]

You can store it like

new_file = "8.2,14.2,-6.1,12.6,7.0,4.7,-1.1,3.6,21.1"

To access any element: Example if you want to access new_file[1][2]

new_file.split(',')[1*total_number_of_columns + 2]

Upvotes: 2

Related Questions