Eliminating Multiple rows using 'if' conditional

2 views (last 30 days)
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

Accepted Answer

the cyclist
the cyclist on 20 Jul 2012
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,:)

More Answers (1)

Walter Roberson
Walter Roberson on 19 Jul 2012
Yes, it is possible to eliminate multiple rows using "if" conditions. For example,
if 1 + 1 == 2
A([3 8:9], :) = [];
end
Often it is cleaner and more efficient to use logical indexing rather than "if" conditions.
The pattern you are looking for seems to be "within each unique value for the first column, if there are more than two consecutive rows with the same value for the third column, then eliminate all except the first and the last of them." Is that what you are looking for?
If so then what should happen if we add an additional entry to the end of the matrix,
2 1725 502
then should there be an implicit re-sorting to move the 1725 to between the 1720 and 1730, thus triggering the 1730 entry to be eliminated? Or since the 1725 entry would be "after" the other entries, should the 1740, 1750, 1760 and 1770 entries all be removed, even though the 1770 is a different column 3, under the rule that "all" entries between the first and last with the same third value should be eliminated?
  2 Comments
MountainKing
MountainKing on 19 Jul 2012
Edited: MountainKing on 19 Jul 2012
Yes, the pattern we are looking for is exactly the one you described: "within each unique value for the first column, if there are more than two consecutive rows with the same value for the third column, then eliminate all except the first and the last of them."
And NO, for all 2s in row1, ONLY the rows below can be deleted:
2 1740 502 %repeat (eliminate this row)
2 1750 502 %repeat (eliminate this row)
CANNOT remove the rows with 1760 & 1770 in col2.
I did not quite understand how implicit re-sorting would help remove all but first & last row for each unique value in Column1.
Note, the values in Column2 for each unique value in Column1 will always be in ascending order.
GOAL: To keep the MIN & MAX value form col2 in case there are intermediate values.
Walter Roberson
Walter Roberson on 19 Jul 2012
min() and max() is a different and easier calculation than removing values positionally. If you want min() and max() you can do your calculation using accumarray()

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!