필터 지우기
필터 지우기

Storing matrices in memory

조회 수: 2 (최근 30일)
Iremsu Savas
Iremsu Savas 2018년 12월 10일
댓글: Walter Roberson 2018년 12월 10일
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.

답변 (1개)

Guillaume
Guillaume 2018년 12월 10일
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
James Tursa
James Tursa 2018년 12월 10일
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 2018년 12월 10일
>> 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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by