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

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 Comments

It's not clear, what are i,j,k,ii,iii? and how many "If" do you have?
please check the question again.. I edited it . the if statements are not determined.. and i,j,ii,iii are index for the for loops
Javier
Javier on 11 Nov 2012
Edited: Javier on 11 Nov 2012
You can use logical operators in Matlab. For example:
x1=(m1==m2). You will get in x1 dummy values (0,1). Now you have the row vector were equality remains. Now redefine x1 as: x1=m1.*x1. This will give you zeros and the values if condition of equality is true.
Hope it helps
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}.
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..

Sign in to comment.

Answers (1)

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 Comment

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..

Sign in to comment.

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Asked:

on 11 Nov 2012

Community Treasure Hunt

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

Start Hunting!