Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matlab cell array size much larger than actual data

I recently discovered an strange behavour of MATLAB cell arrays that was not happening before.

If I create a cell array with

a=cell(1,4)

its size is 32 bytes.

If then I put something inside, e.g.

a{2}='abcd'

its size becomes 144 bytes. But if I remove this content by putting

a{2}=[]

the size becomes 132 bytes and so on. What is the problem?

like image 451
A. Maron Avatar asked Mar 15 '23 14:03

A. Maron


1 Answers

Simply put, the Matlab cell array needs some internal data structures to keep track of what is stored within.

As it seems, Matlab allocates memory as needed, and thus extends the storage needed by the cell array as you insert data.

Removing the data doesn't mean that matlab can return the now unused memory to the OS or internal memory pool -- that might either be something that is impossible with the internal storage structure, or something that would be unwise with respect to performance, because cell arrays from which data is removed are (speaking over all use cases of cell arrays) be structures that get updated often, so that "prematurely" returning memory just to acquire it back again a few instructions later would be pretty CPU-intense.

As a general note: Matlab has pretty terrible storage approaches for nearly everything but matrices and sparse matrices (vectors of course being special cases of matrices). That's because it's not Matlab's job to be e.g. a string parser etc.

If memory becomes a problem, it might be worth considering implementing the math core of your problem in Matlab and doing the rest in other, more generally usable programming languages and somehow interfacing your Matlab code with that -- I haven't tried that myself, but Mathworks has a Matlab engine for python, and I'd take writing python for things like storing arbitrary data over using Matlab every day; with that engine, you can call Matlab to do your dirty math work, and use python to do your everyday scripting/programming work.

Notice that my bottom line here is that Matlab has great Math routines and impressive documentation, but if you want to actually develop software, using a general purpose tool/language is much more likely to be satisfying quickly.

I'd even go as far as saying that it's probably worth your time to learn python, just to be able to circumvent having to deal with things that Matlab wasn't designed for (and cell arrays are a prime example of what Matlab is really complicated about and what's extremely easy in python).

like image 73
Marcus Müller Avatar answered Mar 23 '23 21:03

Marcus Müller