Appending rows/columns to a matrix in MATLAB seems quite something to consider. For example, when I tried appending a column to a matrix A
which has many rows and many columns, like
A = [A, added_col]
Matlab will warn me that since this has to make a copy of A in the memory, I'd better use pre-allocation for speed. This is understandable, because the underlying data in A
occupies a contiguous block of memory.
My question is, will removing rows/columns cause similar issues? For example, to remove the second row of A
:
A(2,:) = []
Is this operation in-place or not? I really feel unsure, since for one thing it doesn't seem to make any new room for data in the memory, and for another, the rows of A
will be stored non-contiguously (since row 2 is removed).
So what will happen internally? And is this operation efficient enough to use in practice? Thanks!
Just tested it with a complexity of 100000
:
clc; clear;
N = 100000;
A = zeros(N, 3);
t1 = tic;
for ii = 1:N
A(ii, :) = [1 2 3];
end
t2 = toc;
And
clc; clear;
N = 100000;
A = zeros(N, 3);
t1 = tic;
for ii = (N-1):-1:2
A(ii, :) = [];
end
t2 = toc;
Results: 0.009s for the first (modifying preallocated matrix) and 53.429s for the second (removing rows from a matrix). I think this basically settles this question: NO, removing rows/columns from a matrix is NOT efficient to use, since it definitely involves deepcopying data and reallocating memory.
Also, removing columns instead of rows isn't a good idea either. As I tested, it still takes as good as approx two minutes, on the above complexity scale:
N = 100000;
test_m = zeros(3, N);
tic
for ii = (N - 1):-1:2
test_m(:, ii) = [];
end
toc
% result: 105.436595 seconds.
% This was run on a different machine than the previous examples.
% But is still enough evidence that dynamically resizing a big matrix is a BAD idea.
So, end of the story: Don't try to remove columns or rows in this way, unless you have a really small matrix. For bulky matrices, always use preallocation instead.