ale
ale

Reputation: 11830

Deleting matrix elements efficiently

I want to efficiently delete a lot of data from the beginning of a matrix of dimension 2*n. The matrix looks like this:

x1 x2
x3 x4
...
...

I want to delete all rows that have the the first element of a row that is smaller than some number and stop when a row isn't smaller (the elements are in numerical order)

What I do at the moment is slow:

while 1 
   if list{i}(1) <= someNumber
      list{i}(1,:) = []
   else
      break;
   end
end

There must be a neat way of doing this quickly in MATLAB?

Thank you.

Upvotes: 0

Views: 413

Answers (1)

Jonas
Jonas

Reputation: 74940

One way is to just compare the entire first column in one go and then delete, i.e.

rows2delete = list{i}(:,1) <= someNumber; %# creates logical array with 1 for deletion
list{i}(rows2delete,:) = []; %# delete some rows, all corresponding cols

Upvotes: 2

Related Questions