MATLAB: Concatenation in Matlab (with or without copying elements)

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?

Best Answer

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.