Concatenation in Matlab (with or without copying elements)
조회 수: 10 (최근 30일)
이전 댓글 표시
Assume we have X = [A, B] where A and B are two vectors (or matrices). Is concatenation operation copies elements in A? If so is it possible to do concatenation without copying elements?
Possible answer to the second. What if I do the following in order to avoid copying elements: X(1:length(A)) = A; X(length(A)+1:length(A)+length(B)) = B; Does 'copy-on-write' work in this context?
Follow-up question: How can I benchmark these two codes with respect to memory usage? Any ideas?
채택된 답변
Jan
2012년 1월 14일
If you concatenate two arrays, the values of both must be copied. The memory used to store the values of an array need to be stored in a continuos block. Therefore even A = [A, B] has to copy all elements of A and B.
The copy-on-write strategy does not matter in this case.
If you have to store large arrays and really need to avoid a copy, use a CELL as a container of the subarrays. Of course any operations using this new object must be re-programmed and will be complicated (slow!), e.g. a matrix-multiplication.
This is even worse:
X(1:length(A)) = A;
X(length(A)+1:length(A)+length(B)) = B;
The indexed copy duplicates A at first. But in the 2nd line, a new larger array is allocated for X and the values of A are copied a second time before B is appended!
It is very hard to define a "memory footprint" exactly, and in consequence the measurement is not trivial also. The JIT might free the memory used by A if it is not used anymore in a function. But if this freed memory is reused to create X depends on the interaction with the operating system. E.g. the current processor load can influence if the memory is cleared and re-used, or if a new block is allocated and the garbage collection happens later.
댓글 수: 0
추가 답변 (1개)
the cyclist
2012년 1월 14일
This page will provide you with a lot of information about memory management in MATLAB: http://www.mathworks.com/support/tech-notes/1100/1106.html
When you do ...
>> X = A;
MATLAB doesn't make a copy of A, until you change an element of X. (Then it copies the entire array.) However, I am pretty sure that that does not apply to concatenation. I don't think there is a way to carry out
>> X = [A,B];
without actually creating the new array in memory.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!