Logical Indexing Flashcards
Use logical indexing to select values greater than 2 in the array below
Data = [1 3 0 8 2 7 1]
Data(data > 2)
What can happen if logical indexing is used on a matrix?
Eg : matrix = [10 20 30; 1 2 3]
Select values >= 10
How to overcome this problem
When logical indexing is used on a matrix, possible that matrix size change due to loss of values
Eg: matrix becomes a [1x3] array
When doing logical indexing, tell MATLAB to fill empty spaces with 0
Matrix(matrix >= 10) = 0
How to use logical indexing to select data in an array
Logical indexing can be used to select data from another variable when the position where data is needed has a corresponding 1 value from the logical index
Eg: index = [1 1 0 1] , data = [3 9 6 7]
Data(index) = [3 9 7]