MATLAB: Eliminating Multiple rows using ‘if’ conditional

for loopif statementmatrices

Is it possible to eliminate multipe rows of a matrix using IF conditional?
A=[1 1875 507
1 1880 508 %repeat



1 1885 508 %repeat (eliminate this row)


1 1890 508 %repeat
1 1895 512
2 1720 501
2 1730 502 %repeat
2 1740 502 %repeat (eliminate this row)
2 1750 502 %repeat (eliminate this row)
2 1760 502 %repeat
2 1770 505]
Here, A(3,:), A(8:9,:) are to be eliminated.
So that the the result would look like:
B=[1 1875 507
1 1880 508
1 1890 508
1 1895 512
2 1720 501
2 1730 502
2 1760 502
2 1770 505]
How can such elimination be done using IF loop for a matrix bigger than this where it is very inefficient to do so for every unique value in Column1?
Thank you

Best Answer

Given your very special requirement for first and last rows, you might be able to use the unique() command. Does this work for you?
[~,indexToFirst] = unique(A(:,[1 3]),'rows','first');
[~,indexToLast] = unique(A(:,[1 3]),'rows','last');
indexToFirstAndLastRows = union(indexToFirst,indexToLast);
A = A(indexToFirstAndLastRows,:)