Matlab cross referencing matrix and table elements

In summary, the conversation discusses a problem with coding in MATLAB involving four column vectors and a matrix. The goal is to add six more columns to the vectors to denote interactions between compounds, with 1 representing an existing interaction and 0 representing no interaction. The solution involves creating a new matrix and copying over the original data.
  • #1
70
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
  • #2
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:
  • #3


It seems like you are trying to find a way to cross reference the elements in your table with the elements in your matrix. One way to do this is to use the "ismember" function in MATLAB. This function allows you to check if an element in one array is present in another array and returns a logical value (1 if present, 0 if not present).

In your case, you could use the "ismember" function to check if the values in your table correspond to the row and column labels in your matrix. For example, for the first reaction in your table (11+4=101+47), you could use the "ismember" function to check if the values 11, 4, 101, and 47 are present in the row and column labels of your matrix. This would give you a logical array with values [1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, ...] which you could then add as additional columns to your table.

You may also want to consider using a different data structure, such as a cell array or a structure, to better organize your data and make it easier to access and manipulate. You could also consider using a "for" loop to iterate through each reaction in your table and use the "ismember" function to check for the presence of interactions in your matrix.

Overall, there are several approaches you could take to solve this problem and it would depend on your specific needs and preferences. I would suggest experimenting with different methods and seeing which one works best for your data and your coding style.
 

1. How do I cross reference elements between a matrix and a table in Matlab?

To cross reference elements between a matrix and a table in Matlab, you can use the "intersect" function. This function takes in two inputs (the matrix and the table) and returns the common elements between them.

2. Can I cross reference specific columns or rows between a matrix and a table in Matlab?

Yes, you can specify which columns or rows you want to cross reference between a matrix and a table in Matlab. This can be done by using the "intersect" function with the additional inputs of the columns or rows you want to compare.

3. How do I handle missing values when cross referencing elements in Matlab?

To handle missing values when cross referencing elements in Matlab, you can use the "setdiff" function. This function takes in two inputs (the matrix and the table) and returns the elements in the first input that are not in the second input. This can help identify missing values that may be causing issues in the cross referencing process.

4. Can I cross reference elements between multiple matrices and tables in Matlab?

Yes, you can cross reference elements between multiple matrices and tables in Matlab by using the "intersect" function with multiple inputs. This function will return the common elements between all of the inputs.

5. Is there a faster way to cross reference elements in Matlab?

Yes, if you are working with large datasets, it may be more efficient to use the "ismember" function instead of the "intersect" function. The "ismember" function takes in two inputs (the matrix and the table) and returns a logical array indicating which elements from the first input are also in the second input. This can be more efficient than using the "intersect" function, especially for larger datasets.

Suggested for: Matlab cross referencing matrix and table elements

Replies
2
Views
941
Replies
3
Views
918
Replies
5
Views
979
Replies
1
Views
63
Replies
3
Views
1K
Replies
10
Views
1K
Replies
4
Views
439
Replies
4
Views
2K
Replies
32
Views
2K
Replies
5
Views
908
Back
Top