Is There Any Practical Difference Between Extraction and Deletion When Reducing a Vector?
Show older comments
By extraction, I mean
% x = x(indexkeep);
By deletion, I mean
% x(indexdelete) = []
Is there any practical difference between these two options (speed, memory management, other?) or is it just a matter of style?
If using numerical indexing, then I suppose it's best to just go with the starting point, e.g., if numerical indexdelete is what we have, then just go with deletion rather than taking the extra step of forming indexkeep for extraction? But if indexdelete is a logical, then converting to indexkeep is trivial if extraction is the better option.
Do locations of the elements to be extracted/deleted matter?
What if using end indexing?
% x = x(n:end); vs.
% x(1:n-1) = [];
As a side note, I came across this behavior that shows a practical difference when deleting/extracting from 2D arrays. Apparently deletion always returns a row vector, whereas extraction returns a row or column depending on the orientation of the index.
xx = rand(2,2);
x = xx; x([1,3]) = []
x = xx; x([1,3]') = []
x = xx; x = x([2,4])
x = xx; x = x([2,4]') % returns a column
x = xx; x([true,false,true,false]) = []
x = xx; x([true,false,true,false]') = []
x = xx; x = x(~[true,false,true,false])
x = xx; x = x(~[true,false,true,false]') % returns a column
3 Comments
Rik
on 5 Oct 2026 at 12:18
If I recall correctly, deletion will work in-place and extraction will not. So extraction will first create a new array of the smaller size and deletion will attempt to edit the original without creating a copy. However, I'm not sure this is true.
I also vaguely recall that Matlab handles the end index in a special way, making it faster than using the numerical last index.
Did you already try to come up with some code snippets to try to build a benchmark to observe the theoretical results?
Paul
on 5 Oct 2026 at 13:33
format debug
a = 1:5
a(5) = []
a = 1:5, a = a(1:4)
Shows both created new allocations; "pr" is the pointer to the real data location in memory.
While it may be physically possible to delete totally in memory, one can't control what MATLAB chooses to do in reallocation at the user level; it is possible with mex to simply change the metadata and not move anything, but one can't prevent it doing whatever it is going to do internally. Internally, the JIT optimizer will decide what is most efficient.
Accepted Answer
More Answers (1)
I remember past tests showing that extraction was more efficient, but that was many years and Matlab versions ago. It still appears to be true, at least in some circumstances. At minimum, this means deletion is not provably better.
N=3e7;
x=rand(N,1);
timeit(@() x(1:N-1),1)
timeit(@() parenDel(x,N),1)
function x=parenDel(x,N)
x(N)=[];
end
function x=parenRef(x,N)
x=x(1:N-1);
end
4 Comments
dpb
on 5 Oct 2026 at 15:34
How much of the difference might be owing to the use of a function in the deletion test as against direct inline instruction for the extraction?
Matt J
on 5 Oct 2026 at 15:41
Fair point. But I've now revised the test so that neither version is inline. The difference is the same.
I suspect that a deletion statement,
x(I)=[];
is implemented internally as,
x = x(setdiff(1:N,I));
The extra overhead of the setdiff operation might be the reason for the slower performance.
N=3e7;
x=rand(N,1);
timeit(@() parenRef(x,N),1)
timeit(@() parenDel(x,N),1)
function x=parenDel(x,N)
x(N)=[];
end
function x=parenRef(x,N)
x=x(1:N-1);
end
Just added the explicit function ref for the extraction; seems to be about 10% slower than the prior inline, maybe. However, this and @Walter Roberson's results below seem in direct contradiction unless I'm misreading them?
On reflection I had come to the conclusion that from a purely straightforward process that the extraction case should generally outperform the deletion method owing to the parsing or RHS vs LHS would create a new memory object as a result of the selection in the former that can then just be assigned to the LHS whereas the [] on the RHS will imply a "move in place" that has to do physical reallocation which has similar issues of copy as is noted in the documentation in the case of preallocation.
I'm confused at this point as to how the two test results come out as they do...
Categories
Find more on Matrix Indexing 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!




