Finding all indices by ismember

user3077261

This is what is described in one of the examples for ismember:

Define two vectors with values in common.

A = [5 3 4 2]; B = [2 4 4 4 6 8];

Determine which elements of A are also in B as well as their corresponding locations in B.

[Lia,Locb] = ismember(A,B)

The result is:

Lia =
     0     0     1     1

Locb =    
     0     0     2     1

The element in B with the lowest index that matches A(3) is B(2). A(4) equals B(1). Is there a way by which we could find all the indices of the elemets of B matching the same element in A?

Eitan T

You can swap the input arguments to ismember:

[tf, ia] = ismember(B, A)

For your example, you should get:

tf =
     1     1     1     1     0     0

ia = 
     4     3     3     3     0     0

This allows you to find, say, the indices of all the elements of B that equal A(3) simply by doing:

find(ia == 3)

Here's a nifty solution for the general case:

[tf, ia] = ismember(B, A);
idx = 1:numel(B);
ib = accumarray(nonzeros(ia), idx(tf), [], @(x){x});

Note that the output is a cell array. For your example, you should get:

ib = 
    []
    []
    [2     3     4]
    [      1]

which means that there are no elements in B matching A(1) and A(2), A(3) matches elements B(2), B(3) and B(4), and A(4) equals B(1).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Finding all indices by ismember

From Dev

Finding indices for all instances of element in array

From Dev

List all index using ismember

From Dev

List all index using ismember

From Dev

Elegant way of finding all integer element indices in a MATLAB array?

From Dev

Elegant way of finding all integer element indices in a MATLAB array?

From Dev

Finding the indices of all matches in a string, appending search term and indices to a dictionary.

From Dev

Finding indices of elements in vector

From Dev

Recursively finding indices of a string

From Dev

Recursively finding indices of a string

From Dev

finding specific indices with pointer array

From Dev

Finding indices of element in array ruby

From Dev

Finding Indices of Unique Elements in R

From Dev

Finding the column indices of submatrices in MATLAB

From Dev

Elasticsearch counts of all indices

From Dev

Finding the indices of the values of one list in another list

From Dev

Finding indices of matches of one array in another array

From Dev

Finding indices that fall between given ranges

From Dev

Finding indices given condition in numpy matrix

From Dev

comparing two lists and finding indices of changes

From Dev

Finding the Indices of multiple items in a list, using a list

From Dev

Finding indices that fall between given ranges

From Dev

Finding start and end indices of positive curves in data

From Dev

R: finding indices of columns in a data.frame

From Dev

Finding all elements with a scroll

From Dev

Finding all empty triangles

From Dev

Finding all subsets of a multiset

From Dev

Finding all possible combinations

From Dev

Finding all quads in a mesh

Related Related

HotTag

Archive