MATLAB Answers

Help with ismember, intersect functions for matching data

23 views (last 30 days)
A
A on 17 May 2011
Hello!
I posted about this before, however the advice I got is still not helping with the problem. I wonder if anyone else might have some insight..
I am working on a program to match cell channels from two difference cell recordings. I have two lists of data, let's call one A, and the other B:
A=[1, 2, 9, 11, 14, 17, 18, 19, 32, 34, 38, 42, 42, 46, 53, 56, 57, 62, 64, 95, 96, 96];
B = [2, 5, 9, 11, 14, 17, 18 , 19, 26, 32, 34, 38, 42, 53, 56, 57, 62, 64, 65, 81, 96];
and the cell number of each is the index value.
(e.g cellA=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22]; cellB =[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21];
What I need to do is determine which values are common between A and B, and return the cell number (location of this channel) so that I can plot a graph just for the common channels.
I have tried using the ismember and the intersect functions:
FIRST ATTEMPT WITH ISMEMBER:
commonA = ismember(A,B); common B = ismember (B,A); %to tell us where repeated values are
cellA=find(commonA); cellB=find(commonB); %to give the index of the repeated value
Which works great until there is a repeated number in A or B, then the indicies no longer match up - for example 42 and 96 are repeated in A, which means that there are two cells firing from each. I need to plot both of these cells against the corresponding cell with channel 42 in B. This method causes the cells to be unmatched after a repeated value.
SECOND ATTEMPT WITH INTERSECT:
[common, iA, iB]=intersect(A,B);
With intersect my indices are nicely matched, however "common" removes the repeated channels, and so I am losing data from one cell if I use this function. (e.g. if 42 occurs at positions 12 and then 13 in A, and at position 13 in B, common only sites that 13A and 13B are common, and ignores that 12A and 13B are common as well).
What I need to return are the indices of A and B for which they have the same example. These should be returned in the form:
indA = [ 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 15, 16, 17, 18, 19, 21, 22]
indB = [ 1, 3, 4, 5, 6, 7, 8, 10, 11, 13, 13, 14, 15, 16, 17, 18, 21, 21]
note that the trick is to keep indA(12) and indB(13) as well as indA(21) and indB(21). The intersect function removes these values and so I am missing data in my return.
Thank you in advance for your help!
  2 Comments
A
A on 17 May 2011
Thank you Leah, I have edited the post to better explain the problem.

Sign in to comment.

Accepted Answer

Matt Fig
Matt Fig on 17 May 2011
Will this do what you want? Note the requirement that A and B are sorted, which you have...
A=[1, 2, 9, 11, 14, 17, 18, 19, 32, 34, 38, 42, 42, 46, 53, 56, 57, 62, 64, 95, 96, 96]
Aidx = 1:length(A);
B = [2, 5, 9, 11, 14, 17, 18 , 19, 26, 32, 34, 38, 42, 53, 56, 57, 62, 64, 65, 81, 96]
Bidx = 1:length(B);
I = ismembc(A,B);
Aintrsct = A(I)
Aidxintrsct = Aidx(I)
For B, then do:
I = ismembc(B,A); % A must be sorted.
Bintrsct = B(I)
Bidxintrsct = Bidx(I)
%
%
%
%
%
%
EDIT Now that we have expected output...
% Data
A = [1, 2, 9, 11, 14, 17, 18, 19, 32, 34, 38, 42, 42, 46, 53, 56, 57, 62, 64, 95, 96, 96]
Aidx = 1:length(A);
B = [2, 5, 9, 11, 14, 17, 18 , 19, 26, 32, 34, 38, 42, 53, 56, 57, 62, 64, 65, 81, 96]
Bidx = 1:length(B);
% Engine
I = ismembc(A,B);
Aidxintrsct = Aidx(I)
Bidxintrsct = arrayfun(@(x) Bidx(x==B),A,'Un',0);
Bidxintrsct = [Bidxintrsct{:}]
  4 Comments
A
A on 17 May 2011
Sorry, I was saying cells because that's what the data is for (and I'm a bit confused myself ;) )
This gives me the right output, thank you so much for your help!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by