MATLAB Matlab cross referencing matrix and table elements

Click For Summary
The discussion revolves around a MATLAB coding challenge involving four column vectors representing chemical reactions and a matrix that indicates possible interactions between compounds. The user seeks assistance in adding six columns to the vectors, where each new column indicates the presence of specific interactions based on the matrix. The user describes the reactions and their classifications, highlighting the need to reference indices correctly between the vectors and the matrix. Suggestions include creating a new matrix for expanded data storage and copying existing data into it. The user expresses uncertainty about efficiently implementing the cross-referencing logic and seeks methodologies to streamline the process. A reference to MATLAB documentation on resizing matrices is provided as a potential resource for further guidance.
hoffmann
Messages
65
Reaction score
0
hi all, here's an interesting problem I've been working on in MATLAB. any help would be appreciated.

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!
 
Physics news on Phys.org
I'm not so sure about efficient, but oh well. One way of doing this would be to create a new 2D matrix called table2 using the zeros instruction:

>> table2=zeros(111, 10);

or, if you were looking for more expandability:

>> table2=zeros((size(table1))(1),(size(table1))(2)+4);

and then copy over table1 to table2:

>> table2(1:111, 1:6) = table1

I'm unaware of any function that allows you to EASILY tack on extra columns (I suppose you could transpose, add some rows by putting zeros in the new rows, and then transpose back) but the following Mathworks Matlab documentation page may nevertheless be of some benefit to you (Resizing and reshaping matrices):

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f1-85766.html
 
Last edited by a moderator:

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 41 ·
2
Replies
41
Views
10K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K