Concatenation in Matlab (with or without copying elements)
Show older comments
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
More Answers (1)
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 Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!