Reducing a very long vector

14 views (last 30 days)
Borislav
Borislav on 10 Dec 2012
I would like to reduce a very long vector without using any (significant amount of) additional memory. For instance, suppose I have slightly over 6GB available and use 6GB of it for a vector A of size [2*N,1]. After some calculations I would like to keep only the first N entries of A. I tried
A = A(1:N, 1);
but it appears that for a short period I will need additional 3GB (which I do not have) as if the first half of A is copied somewhere else and then back. I am looking for a way to move the end-of-array pointer of A to the middle of A and release the second half of A.
Thank you, Borislav

Accepted Answer

Walter Roberson
Walter Roberson on 10 Dec 2012
MATLAB does not provide any explicit way to adjust the sizes.
Your best chance (not guaranteed at all) would be to write an auxillary function,
function A = chopvec(A, N)
A = A(1:N);
end
MATLAB is able to notice that the input and output array names are the same, and can do some kinds of work "in place" (provided the input array was not a shared one.) I do not know if shortening is one of the things it can do. Possibly not.
Ah.... did you try
A(N+1:end) = [];
?
  6 Comments
Matt Fig
Matt Fig on 12 Dec 2012
I think you are correct, Walter. I found the thread I was remembering and it had nothing to do with this topic. Oh well.
Jan
Jan on 12 Dec 2012
What thread are you talking of? I think it is possible, that I had posted more than one, perhaps two or even three.
Hacking the dimensions inplace is safe (as long as undocumented feature can be). Even with shared variables this does neither crash nor leak memory: mxSetDimension with a smaller number of elements.
But using mxRealloc to release the ununsed memory is a bad idea, when the variable is shared. Although the functions mxUnshare, mxUnshareArray and mxUnreference should cut the connection to a shared variable successfully, I strongly recommend not to apply this in a productive system.
Please send an enhancement request to TMW and ask for the documentation of the sharing mechanism in MEX functions. We all know that the shared-data-copy system is stable and efficient. Therefore it would be a great benefit for MEX programmers to be able, to use this feature for efficiency.

Sign in to comment.

More Answers (0)

Categories

Find more on Operators and Elementary Operations 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!