Reputation: 11
Given a vector X
of discrete positive integers with size 160*1, and a table Tb1
in size 40*200, that contains a list of indices to be deleted from X
Each column from the 200 columns in Tb1
points to 40 elements to be deleted from original X
.
I create a new matrix of the remaining 120*200 elements by using a for
loop with 200 iterations, that at round i
deletes 40 elements from a copy of the original X
according to the indices listed in Tb1(:,i)
, but it takes too much time and memory.
How can I get the result without using loops and with a minimum number of operations?
Upvotes: 1
Views: 61
Reputation: 15837
Here are different methods:
Method1:
idx = ~hist(tbl, 1:160);
[f,~]=find(idx);
result1 = reshape(M(f),120,200);
Method2:
idx = ~hist(tbl, 1:160);
M2=repmat(M,200,1);
result2 = reshape(M2(idx),120,200);
Method 3 & 4:
% idx can be generated using accumarray
idx = ~accumarray([tbl(:) reshape(repmat(1:200,40,1),[],1)],true,[160,200],@any);
%... use method 1 and 2
Method5:
M5=repmat(M,200,1);
M5(bsxfun(@plus,tbl,0:160:160*199))=[];
result5 = reshape(M5,120,200);
Assuming that M
is an array of integers and tbl
is the table of indices.
It can be tested with the following data:
M = rand(160,1);
[~,tbl] = sort(rand(160,200));
tbl = tbl(1:40,:);
However it is more efficient if you generate indices of elements to be remained instead of indices of elements to be removed.
Upvotes: 1