Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
cross referencing columns and matrices

Subject: cross referencing columns and matrices

From: Pr B

Date: 20 Feb, 2009 21:46:02

Message: 1 of 8

i have four column vectors of length ~11000 (table1):

in1 in2 out1 out2
11 4 101 47
1 8 111 9
38 1 1 39
...

i also have a matrix with dimensions 111x111 (matrix1):

        c1 c2 c3 c4 ...
c1 0 0 0 1
c2 0 0 1 1
c3 0 1 0 0
c4 1 1 0 0
...

the first four column vectors in table1 represent reactions (read across columns) in which compounds 1 through 111 can participate in. compound 1 in table1 represents a compound with zero mass, so essentially a nonexistent compound. e.g. 11+4=101+47 is the reaction contained in the first row of table 1.

the first reaction in table1 can be classified as A+B=C+D with six possible interactions between the compounds: A-B, A-C, A-D, B-C, B-D, C-D. the second reaction can be classified as A=C+D with its 3 appropriate interactions. the third reaction can be classified as A=D with its 1 interaction.

i basically would like to add 6 more columns to table1 denoting whether an interaction existing in the reaction exists in matrix1. put a 1 in the column when an interaction exists and a 0 otherwise. i need to do this for all reactions in table1. the values in table1 denote the indices of the compounds which should correspond to the row and column labels in matrix1 (i basically added a 'c' tag in front for readability).

i'm having difficulty coding this problem. i would approach it using something like:
for i=1:length(in1)
if matrix1(A,B)==1 && table1(A,B)~=0 %here's where it gets iffy for me
% i'm unsure on how to create columns and make sure i am cross referencing the %elements of table1 and matrix1 properly.

i would appreciate any help since i've tried to conceptualize this problem and believe i am doing so correctly. any more efficient methodologies would be welcome. thanks!

Subject: cross referencing columns and matrices

From: Pr B

Date: 21 Feb, 2009 00:00:05

Message: 2 of 8

clarification:

the table contains 111 compounds from 1-111, with 1 being the compound with zero mass.

the matrix is 110x110 with no row and column for the compound with zero mass. it doesn't really need to be there. this matrix is symmetric.

Subject: cross referencing columns and matrices

From: Pr B

Date: 21 Feb, 2009 02:45:04

Message: 3 of 8

for i = 1: size(table1,1)
for j = 1:size(matrix,1)
if sum(table(i,:)==matrix(j,:))==4
zerovector(i)=1
end
end
end

this fails, however, and also cannot be too efficient. any ideas?

Subject: cross referencing columns and matrices

From: Roger Stafford

Date: 21 Feb, 2009 12:53:01

Message: 4 of 8

"Pr B" <pb2297@columbia.edu> wrote in message <gnn8aq$9sn$1@fred.mathworks.com>...
> i have four column vectors of length ~11000 (table1):
>
> in1 in2 out1 out2
> 11 4 101 47
> 1 8 111 9
> 38 1 1 39
> ...
>
> i also have a matrix with dimensions 111x111 (matrix1):
>
> c1 c2 c3 c4 ...
> c1 0 0 0 1
> c2 0 0 1 1
> c3 0 1 0 0
> c4 1 1 0 0
> ...
>
> the first four column vectors in table1 represent reactions (read across columns) in which compounds 1 through 111 can participate in. compound 1 in table1 represents a compound with zero mass, so essentially a nonexistent compound. e.g. 11+4=101+47 is the reaction contained in the first row of table 1.
>
> the first reaction in table1 can be classified as A+B=C+D with six possible interactions between the compounds: A-B, A-C, A-D, B-C, B-D, C-D. the second reaction can be classified as A=C+D with its 3 appropriate interactions. the third reaction can be classified as A=D with its 1 interaction.
>
> i basically would like to add 6 more columns to table1 denoting whether an interaction existing in the reaction exists in matrix1. put a 1 in the column when an interaction exists and a 0 otherwise. i need to do this for all reactions in table1. the values in table1 denote the indices of the compounds which should correspond to the row and column labels in matrix1 (i basically added a 'c' tag in front for readability).
>
> i'm having difficulty coding this problem. i would approach it using something like:
> for i=1:length(in1)
> if matrix1(A,B)==1 && table1(A,B)~=0 %here's where it gets iffy for me
> % i'm unsure on how to create columns and make sure i am cross referencing the %elements of table1 and matrix1 properly.
>
> i would appreciate any help since i've tried to conceptualize this problem and believe i am doing so correctly. any more efficient methodologies would be welcome. thanks!
--------------------
  It would be advisable to extend matrix1 to include the "zero mass" compound in its first row and column as all zeros, making its augmented size 111x111. This makes the values in table1 match the indices in matrix1, as well as simplifying the computation. I assume here this has been done. For brevity let T be the table1 array and let M be the (augmented) matrix1 array.

  Then the six columns to be appended to T (table1) for the six possible interactions can be obtained by:

 AB = M(sub2ind(size(M),T(:,1),T(:,2)));
 AC = M(sub2ind(size(M),T(:,1),T(:,3)));
 AD = M(sub2ind(size(M),T(:,1),T(:,4)));
 BC = M(sub2ind(size(M),T(:,2),T(:,3)));
 BD = M(sub2ind(size(M),T(:,2),T(:,4)));
 CD = M(sub2ind(size(M),T(:,3),T(:,4)));

Roger Stafford

Subject: cross referencing columns and matrices

From: Pr B

Date: 21 Feb, 2009 17:22:02

Message: 5 of 8

hi roger, thanks for your reply.

so basically you're saying, add in the row and column for the zero mass compound and then run your code? or do i run it within the for loop i wrote above? basically, i'm not seeing where your code tests if the interactions in table1 lie within matrix1.

Subject: cross referencing columns and matrices

From: Pr B

Date: 21 Feb, 2009 17:39:01

Message: 6 of 8

here's what i assume you mean:

A=[zeros(length(A),1) A];
A=[zeros(1,length(A));A];

AB = M(sub2ind(size(M),T(:,1),T(:,2)));
AC = M(sub2ind(size(M),T(:,1),T(:,3)));
AD = M(sub2ind(size(M),T(:,1),T(:,4)));
BC = M(sub2ind(size(M),T(:,2),T(:,3)));
BD = M(sub2ind(size(M),T(:,2),T(:,4)));
CD = M(sub2ind(size(M),T(:,3),T(:,4)));

Subject: cross referencing columns and matrices

From: Pr B

Date: 21 Feb, 2009 18:53:01

Message: 7 of 8

ah, i get it. sub2ind is nifty. thanks, roger.

Subject: cross referencing columns and matrices

From: Pr B

Date: 23 Feb, 2009 19:28:02

Message: 8 of 8

how would i go about doing a case by case sum on the results of the table?

what i mean is, for a reaction with zero 1 entries, is the reaction viable? this means >=3/6 possible interactions in columns 5-10 are present.

for a reaction with one 1 entry, is the reaction viable? this means >=2/3 possible interactions in columns 5-10 are present.

for a reaction with two 1 entries, is the reaction viable? this means 1/1 possible interactions in columns 5-10 are present.

i'm assuming a for loop with a few if statements would work, but is there a more elegant way to do it?

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us