Pitlab
Pitlab

Reputation: 23

Removing rows based on a condition (Matlab)

I am trying to remove rows from a matrix based on a condition. I have a 371000x5 double matrix, and a 371000x1 vector of dummies (1 and 0). I want to remove each row from the original matrix, where the value of the vector of dummies is 1. I have tried the following, but it is taking very long:

for i = 1:size(matrix_num,1)
if missing_matrix(i,1) >=0
    matrix_num(i,:) = [];
end
end

My Matlab has been busy for over 30 minutes now, so I am not even sure if the code is right. Is there a more efficient way to do this?

Additionally, I have to do the same action on a cell matrix (categorical data). Should I expect any huge difference from the numerical matrix?

Upvotes: 0

Views: 397

Answers (2)

mousomer
mousomer

Reputation: 2848

The programmatic way of doing this is:

new_matrix = old_matrix(missing_vector==1,:)

for keeping lines with missing_vector 1

new_matrix = old_matrix(missing_vector==0,:)

for removing lines with missing_vector 1

For educational values, if you want the loop to work, don't do that row by row. Your solution causes the matrix to be copied and re-allocated on every row removed.

So, you would be better off if you calculate the resulting matrix size in advance:

new_matrix = zeros(sum(missing_vector), 5)

and then your iteration would work:

index_new=1
for index_old = 1:size(old_matrix,1)
      if missing_vector(index_old) ==0
            new_matrix(index_new,:) = old_matrix(index_old,:);
      end
end

Upvotes: 1

PyMatFlow
PyMatFlow

Reputation: 478

Try compact MATLAB code

matrix_num(missing_matrix>=0,:)=[]

  • Note : You must make a vector for missing_matrix variable. If this variable is matrix, you need other form of code .

As I know, you can use it in cell array too.

Upvotes: 0

Related Questions