Concatenation in Matlab (with or without copying elements)
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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?
Akzeptierte Antwort
Jan
am 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.
0 Kommentare
Weitere Antworten (1)
the cyclist
am 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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!