Is there a fast way to remove rows and columns from a large matrix in MATLAB?
I have a very large (square) distance matrix, that I want to remove a number of rows/co
It seems like a memory bottleneck. On my feeble laptop, breaking D up and applying these operators to each part was much faster (using s=12,000 crashed my computer). Here I break it into two pieces, but you can probably find a more optimal partition.
s = 8000;
D = rand(s);
D1 = D(1:s/2,:);
D2 = D((s/2 + 1):end,:);
cols = sort(randsample(s,2));
rows = sort(randsample(s,2));
A1 = D1;
A2 = D2;
tic
A1(rows(rows <= s/2),:) = [];
A2(rows(rows > s/2) - s/2,:) = [];
A1(:,cols) = [];
A2(:,cols) = [];
toc
A = D;
tic
A(rows,:) = [];
A(:,cols) = [];
toc
Elapsed time is 2.317080 seconds.
Elapsed time is 140.771632 seconds.
I think it will depend on your usage, but I have two ideas:
A = D(randsample(s,2), randsample(s,2));
clear D;
% Use A