Suppose we have this matrix

1 2 3 4 5 6 7 8 |
Matrix = 35 1 6 26 19 24 3 32 7 21 23 25 31 9 2 22 27 20 8 28 33 17 10 15 30 5 34 12 14 16 4 36 29 13 18 11 |

and we need to search for the values

1 2 3 |
SearchValues = 3 30 10 |

to get the corresponding row numbers of the matrix in case *at least one of the values* is found in that row. The result should look like the following:

1 2 3 |
RowNumbers = 2 5 4 |

Let’s see the code snippet performing this task using the above defined vectors:

1 2 3 4 5 6 7 |
Matrix = magic(6); SearchValues = [3 30 10]; RowNumbers = []; for i = 1:length(SearchValues) RowNumbers = [RowNumbers find(sum(Matrix == SearchValues(i), 2))]; end |

## Leave a Reply