Concatenation in Matlab (with or without copying elements)

10 views (last 30 days)
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?

Accepted Answer

Jan
Jan on 14 Jan 2012
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.

More Answers (1)

the cyclist
the cyclist on 14 Jan 2012
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.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!