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
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}.
回答(1 个)
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!