user1166251
user1166251

Reputation: 115

find similar values and delete row if difference is too small

I have a matrix A(x y z) in Matlab

I would like to delete the rows of the z values too similar to the previous one. So

for i = 2 : length (A(:,1))
   if abs(A(i,3)-A(i-1,3)) <= 0.087
      A(i,:) = [];
   end
end

I have an error 'Index exceeds matrix dimensions.'.

Is there a different way to do it?

Upvotes: 1

Views: 191

Answers (1)

chappjc
chappjc

Reputation: 30579

One idea for removing the rows, using a threshold of 0.2 on the third column, for example:

removeMask = [false; abs(diff(A(:,3))) < 0.2];
A(removeMask,:) = []

Example:

>> A = rand(10,3)
A =
    0.4173    0.7803    0.2348
    0.0497    0.3897    0.3532
    0.9027    0.2417    0.8212
    0.9448    0.4039    0.0154
    0.4909    0.0965    0.0430
    0.4893    0.1320    0.1690
    0.3377    0.9421    0.6491
    0.9001    0.9561    0.7317
    0.3692    0.5752    0.6477
    0.1112    0.0598    0.4509
>> removeMask = [false; abs(diff(A(:,3))) < 0.2]
removeMask =
     0
     1
     0
     0
     1
     1
     0
     1
     1
     1
>> A(removeMask,:) = []
A =
    0.4173    0.7803    0.2348
    0.9027    0.2417    0.8212
    0.9448    0.4039    0.0154
    0.3377    0.9421    0.6491

Upvotes: 3

Related Questions