indexing duplicates in cell arrays
Show older comments
im currently attempting to locate small atomic features within a large atomic structure using matlab.
This involves, calculating interatomic distances, defining bonds etc etc which has all been done. The final piece of the puzzle is what is troubling me. The feature in question is a diamond shaped planar fragment i.e. a single atom bonded to two atoms which are in turn bonded to a 4th atom. The way i attacked this problem was by finding all atoms bonded to at least 2 others, then determing which atoms those bonds are to, then determine what atoms those are in turn bonded atom. This means i start with a single number( which identifies the atoms) which is bonded to an 1D array and that array is leads to a 2D array all of which i place into N*3 cell array (where N is the number of atoms with more than 2 bonds).
so a typical line looks like this
2 [61;85;88] [3x1 cell]
where the 3x1 cell looks like this
[2;44;85;88]
[2;19;34;44;61;74]
[2;23;61]
what i need to do now is search this 3x1 cell array (the dimention isnt fixed at 3 it can vary from 2 upto 10+) for duplicate entries which arent equal to the value in the first cell i.e. 2. Once the duplicates are found i need to also determine which of the cells there are in so i know which atom the duplicates belong too. For example in this case atom 2 is bonded to 61 and 85 which are both in turn bonded to 44, similarly 2 is bonded to 85 and 88 which are bonded to 61.
unfortunately im qutie new to matlab and coding in general so i dont know how to solve this problem. any help would be appriciated
thank you
2 Comments
Sean de Wolski
on 10 Oct 2011
Can you show the output you expect for the above example please?
kieth
on 11 Oct 2011
Answers (2)
Fangjun Jiang
on 10 Oct 2011
A_ID=2;
B=[61;85;88];
C={[2;44;85;88],[2;19;34;44;61;74],[2;23;61]};
Out=[];
B_2=nchoosek(B,2);
for k=1:size(B_2,1)
for M=1:length(C)
M_Cell=[];
if ~ismember(B_2(k,1),C{M})
continue;
else
M_Cell=C{M};
end
for N=1:length(C)
N_Cell=[];
if M==N || ~ismember(B_2(k,2),C{N})
continue
else
N_Cell=C{N};
end
Common=intersect(M_Cell,N_Cell);
Common=setdiff(Common,A_ID);
if ~isempty(Common)
Out=[Out;B_2(k,:),Common];
end
end
end
end
>> Out
Out =
61 85 44
61 88 44
2 Comments
kieth
on 11 Oct 2011
Fangjun Jiang
on 11 Oct 2011
I can't seem to understand how your result should come out. You'll have to explain in details.
Daniel Shub
on 10 Oct 2011
0 votes
Your description sounds an awful lot like a linked list to me.
It seems your current data structure is such that it is causing problems. I think if you get the structure of your data squared away, the calculation will be easier.
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!