Finding all indices by ismember
Categories:
Efficiently Finding All Indices with MATLAB's ismember

Discover how to leverage MATLAB's ismember
function to find all occurrences of elements within an array, going beyond its default behavior to retrieve every matching index.
MATLAB's ismember
function is a powerful tool for identifying whether elements of one array are present in another. By default, it returns a logical array indicating membership and, optionally, the first index of each matching element. However, many users need to find all indices where a match occurs, not just the first. This article will guide you through various techniques to achieve this, from basic ismember
usage to more advanced methods for retrieving every single matching index.
Understanding ismember
Basics
The ismember
function has two primary output forms. When called with one output, L = ismember(A, B)
, it returns a logical array L
of the same size as A
, where L(i)
is true if A(i)
is present in B
. When called with two outputs, [L, loc] = ismember(A, B)
, it also returns loc
, an array of indices. For each element A(i)
that is a member of B
, loc(i)
is the smallest index in B
such that B(loc(i))
equals A(i)
. If A(i)
is not a member, loc(i)
is 0. This loc
output is often where the confusion arises for users seeking all indices.
A = [1 2 3 2 4 5 2];
B = [2 5];
[L, loc] = ismember(A, B);
disp('Logical array L:');
disp(L);
disp('Location array loc (first occurrence):');
disp(loc);
Basic usage of ismember
showing logical output and first occurrence indices.
Finding All Indices for a Single Value
If you're looking for all occurrences of a specific single value within an array, the simplest approach is to use logical indexing directly. This method is straightforward and highly efficient for individual elements.
myArray = [10 20 30 20 40 50 20];
valueToFind = 20;
allIndices = find(myArray == valueToFind);
disp(['All indices for value ' num2str(valueToFind) ':']);
disp(allIndices);
Using find
with logical indexing to get all occurrences of a single value.
Finding All Indices for Multiple Values Using ismember
and find
When you need to find all indices for multiple values present in a lookup array, you can combine ismember
with find
. The ismember
function will give you a logical array indicating where any of the target values exist. Then, find
can convert this logical array into linear indices.
flowchart TD A[Input Array (A)] --> B[Values to Find (B)] B --> C{"ismember(A, B)"} C --> D[Logical Array (L)] D --> E{"find(L)"} E --> F[All Matching Indices]
Workflow for finding all indices of multiple values using ismember
and find
.
sourceArray = [11 22 33 22 44 55 22 11 66];
targetValues = [22 11];
logicalMatches = ismember(sourceArray, targetValues);
allMatchingIndices = find(logicalMatches);
disp('Source Array:');
disp(sourceArray);
disp('Target Values:');
disp(targetValues);
disp('Logical Matches:');
disp(logicalMatches);
disp('All Matching Indices:');
disp(allMatchingIndices);
Combining ismember
and find
to get all indices for multiple target values.
containers.Map
or accumarray
could offer performance benefits, especially if you need to group indices by value.Advanced: Grouping Indices by Value
Sometimes, you don't just need all indices, but you need them grouped by which target value they correspond to. While ismember
doesn't directly provide this, you can achieve it with a loop or by using accumarray
for a more vectorized approach, especially if your values are positive integers.
sourceArray = [11 22 33 22 44 55 22 11 66];
targetValues = [22 11];
% Method 1: Loop through target values
indicesByValue = containers.Map('KeyType', 'double', 'ValueType', 'any');
for i = 1:length(targetValues)
currentValue = targetValues(i);
indicesByValue(currentValue) = find(sourceArray == currentValue);
end
disp('Indices grouped by value (Loop method):');
disp(indicesByValue);
% Method 2: Using accumarray (if values are suitable for indexing)
% This example assumes targetValues are positive integers for simplicity
% If not, you might need to map them to indices first.
% Example with different sourceArray for accumarray suitability
sourceArray_acc = [1 2 3 2 1 4 2];
targetValues_acc = [1 2];
% Create a mapping from targetValues to 1, 2, ... for accumarray
[~, ~, targetMap] = unique(targetValues_acc);
% Initialize a cell array to store results
resultCell = cell(length(targetValues_acc), 1);
for i = 1:length(targetValues_acc)
resultCell{i} = find(sourceArray_acc == targetValues_acc(i));
end
disp('Indices grouped by value (Manual accumarray-like logic):');
for i = 1:length(targetValues_acc)
fprintf('Value %d: [', targetValues_acc(i));
fprintf('%d ', resultCell{i});
fprintf(']\n');
end
Grouping all matching indices by their corresponding value using a loop and a conceptual accumarray
-like approach.
accumarray
function is powerful but requires its first argument to be positive integer subscripts. If your target values are not positive integers, you'll need an intermediate step to map them to valid indices before using accumarray
.