Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab cross referencing matrix and table elements

  1. Feb 20, 2009 #1
    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!
  2. jcsd
  3. Feb 22, 2009 #2


    User Avatar
    Science Advisor

    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 [Broken]
    Last edited by a moderator: May 4, 2017
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook