Eliminating Multiple rows using 'if' conditional
Mostra commenti meno recenti
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
Risposta accettata
Più risposte (1)
Walter Roberson
il 19 Lug 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 Commenti
MountainKing
il 19 Lug 2012
Modificato: MountainKing
il 19 Lug 2012
Walter Roberson
il 19 Lug 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()
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!