how to get rid of the too many if statements..

3 次查看(过去 30 天)
I have 2 cell matrix (coming from a certain code):
m1 =
'GO:0008150' 'GO:0016740'
'GO:0016740' 'GO:0016787'
'GO:0016787' 'GO:0006810'
'GO:0008150' 'GO:0006412'
'GO:0016740' 'GO:0004672'
'GO:0016740' 'GO:0016779'
'GO:0016787' 'GO:0004386'
'GO:0016787' 'GO:0003774'
'GO:0016787' 'GO:0016298'
'GO:0006810' 'GO:0016192'
m2 =
'GO:0008150' 'GO:0016787'
'GO:0008150' 'GO:0006810'
'GO:0006810' 'GO:0006412'
'GO:0016192' 'GO:0003774'
'GO:0006810' 'GO:0005215'
'GO:0005215' 'GO:0030533'
in each matrix, the first column is the parent of the second one... I need from the code to find the part of graph which can determine all relations between these cells... I have to find cell from column 1 that occurance in column 2, then take the cell from column 1 but from the same row of column 2 and find the similarity between it and onother cell in column 2 .. and so.. it is something like depth first search in graph theory
In general, How can I make a dynamic code (recursive) that can replace the following if statements:
for k=1:length(m1)
for ii=1:length(m1)
for j=1:length(m1)
for i=1:length(m2)
for e=1:length(m1)
if isequal(m2{i,1},m1{j,2})
x1=[m1(j,1) m2(i,2)];
x11=[x1;x11];
if isequal(m1{j,1},m1{k,2})
x2=[m1(k,1),m2(i,2)];
x22=[x2;x22];
if isequal(m1{k,1},m1{ii,2})
x3=[m1(ii,1),m2(i,2)];
x33=[x3;x33];
if isequal(m1{ii,1},m1{e,2})
x4=[m1(e,1),m2(i,2)];
x44=[x4;x44];
.
.
and so..
x_total=[x11;x22;x33;x44...]
end
end
end
end
end
end
end
Note that the if statemtnes are not determined( it depends on m1 & m2 which are also not always constants) I think I have to use recursion technique..
  5 个评论
Arthur
Arthur 2012-11-11
Why do you loop 4 times over the same array? Every if statement does exactly the same thing. What do you want to achieve with this script???
Note not that you can also use strcmp to find matching strings in a cell array. For instance:
idx = strcmp(m1{1},m1);
will find you all values matching m1{1}.
Jwana
Jwana 2012-11-12
The first column is the parent of the second one... I need from these if statements to find the part of graph which can determine all relations between these cells... it is not the same value every time you search in m1 cells array.. you have to find cell from column 1 that occurance in column 2, then the take the cell from column 1 but from the same row of column 2 and find the similarity between it and ontother cell in column 2 .. and so..

请先登录,再进行评论。

回答(1 个)

Star Strider
Star Strider 2012-11-11
编辑:Star Strider 2012-11-11
I am not certain what you want to do, so I am guessing here.
Would something like this do what you want:
[C ia ic] = intersect( m1(:,1), m1(:,2) );
The intersect function looks for occurrences common to both columns in your m1 array. you then only need to look for occurrences of the same index in ia and ic to find values that are common to both columns in the same rows.
  1 个评论
Jwana
Jwana 2012-11-13
Thank you for your response... The first column is the parent of the second one... I need from these if statements to find the part of graph which can determine all relations between these cells... it is not the same value every time you search in m1 cells array.. you have to find cell from column 1 that occurance in column 2, then the take the cell from column 1 but from the same row of column 2 and find the similarity between it and ontother cell in column 2 .. and so..

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by