Avoid copy when slicing large vector

6 ビュー (過去 30 日間)
Son Hua
Son Hua 2011 年 3 月 9 日
We know that MATLAB employs the copy-on-write mechanism for handling arrays. For example
a = zeros(10^6);
b = a; % Just a pointer assignment.
b = a(123:end); % Slicing causes b to copy values from a.
My question is, can I have something similar to b = a(123:end), but no copy is performed? My vector a is quite large (much larger than 10^6 elements), so slicing causes overhead. Can I create a C MEX-file that assign a pointer of a+123 to b and return the array without copying?
Thanks.

回答 (1 件)

Jan
Jan 2011 年 3 月 9 日
You can look into inplacecolumnmex.c of Bruno's http://www.mathworks.com/matlabcentral/fileexchange/23576-minmax-selection .
It works, but has a limited stability only: If the program stops with an error, before the arrays are freed, Matlab crashs due to a confused memory manager.
If you are familar with C-Mex files, implementing the functions operating on your intermediate arrays, would be much more reliable.

カテゴリ

Help Center および File ExchangeProgramming Utilities についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by