This seems like a simple question but I have been unable to find an answer anywhere. If I have a Matlab matrix A consisting of an arbitrary number of rows, how would I filter these rows based on the value of some function f (the argument of which is a row vector)? In other words, how would I keep only the rows of matrix A for which f is true? I tried
A(f(A(:)), :)
but to no success. Any help would be greatly appreciated.
As Dan answered, you can filter the rows of a matrix according to a logical vector. Let
selectRowsbe a logical vector withnumel(selectRows) == size(A,1)withtruefor rows to be kept, andfalsefor rows to be discarded. Then:Will remove all rows for which
selectRows == false.Now, the question is how to generate the logical vector
selectRowsusing the functionf?If
fknows how to process multiple rows, and return multipletrue/falsevalues accordingly, thenShould do the trick.
However, if
fonly knows how to process one row at a time you will need to loop through the rows. One way of doing so would be