Borrar filtros
Borrar filtros

Storing matrices in memory

2 visualizaciones (últimos 30 días)
Iremsu Savas
Iremsu Savas el 10 de Dic. de 2018
Comentada: Walter Roberson el 10 de Dic. de 2018
Hello everyone,
I wanted to ask: if I have a matrix with size 4x4, and if i have 4 other matrices with size 1×4.Does the space that they take in the computer memory change? if so what is the difference for keeping a 4x4 matrix and different four matrices with the size 1x4 in the memory?
I hope everything is clear.
thank you.

Respuestas (1)

Guillaume
Guillaume el 10 de Dic. de 2018
Storing 4 matrices takes slightly more space than 1. For each matrix, you not only need to store the elements, but also some metadata such as its type, number of dimensions and size of each dimension. However, the difference is so negligible that you should not worry about it. If you are so short of memory that the few bytes of difference are critical, you probably have deeper problems (as in you can't even do any more calculations).
  3 comentarios
James Tursa
James Tursa el 10 de Dic. de 2018
The best thing to do is use the profiler on your code to see if it is a bottleneck (probably not). If you still have questions, then post your code here and we can comment on it and make suggestions.
Walter Roberson
Walter Roberson el 10 de Dic. de 2018
>> size4x4 = zeros(4,4)
size4x4 =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
>> size1x4 = zeros(1,4)
size1x4 =
0 0 0 0
>> whos
Name Size Bytes Class Attributes
size1x4 1x4 32 double
size4x4 4x4 128 double
so according to Bytes, size4x4 is 4 times larger than size1x4 . But as Guillaume indicates, you have to add the overhead for each matrix. That is approximately 112 bytes.
Remember that if you have an expression such as size4x4(3, 1) to select row 3, then in most cases, MATLAB constructs a temporary matrix to hold the 1 x 4, and that temporary matrix internally needs the approximately 112 bytes as well. Therefore you are not necessarily saving anything useful at run-time by merging the data into a 4x4 if you are always extracting 1x4 out of it to do whatever work is appropriate.
If you were generating C code, then in some read-only cases, it could be more space efficient to have the data packed into the 4 x 4.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by