HKhoshdel
HKhoshdel

Reputation: 33

matlab: deleting rows of a matrix given some index

I have a big matrix A and I want to delete some rows of it which their indexes are in another vector V. that vector is unsorted so the code below won't work

for i=1:length(V)
     A(V(i))=[]
end

the problem is when for example row 2 is deleted, and next I want to delete row 4, it will delete row 5 instead, cause index of row 5 is now 4. i think it is possible to sort v descending and delete like above but that sorting is time consuming. is there any other to do this task?

Upvotes: 1

Views: 89

Answers (1)

crazyGamer
crazyGamer

Reputation: 1139

You just need to pass the entire vector of indices to delete.

MATLAB supports very powerful indexing features, and learning them could simplify writing code and also make them less error-prone.

Sample code

% Delete all rows with row indices as in V
A(V, :) = []

Upvotes: 1

Related Questions