MATLAB Answers

0

How can I compare two cell arrays inside two different structures?

Asked by Bhushan Dhamale on 18 May 2018 at 13:42
Latest activity Commented on by Ameer Hamza
about 22 hours ago

I have two structures model and dataMtb. I want to compare the fields model.grRules and dataMtb.gene having dimensions 1128*1 and 4109*1 respectively. I want to create a cell array which would give me a list of only those genes which are common between the two cell arrays. I am attaching the .mat files for reference. Please note that the cells in model.grRules cell array may contain names of more than one gene in each cell. How do I achieve the above objective?

  0 Comments

Sign in to comment.

Products

2 Answers

Answer by Ameer Hamza
on 18 May 2018 at 17:01
Edited by Ameer Hamza
on 18 May 2018 at 17:01
 Accepted Answer

This code will extract common values of genes which occur in either of the vectors.

list1 = string(dataMtb.gene);
list2 = string(iEK1011.grRules);
commonValues = list1(cellfun(@(x) any(contains(list2, x)), list1));

  2 Comments

Sign in to comment.


Answer by Paolo
on 18 May 2018 at 17:59
Edited by Paolo
on 21 May 2018 at 11:44

Hi Bhushan,

This code will split the cells in model.grRules which contain multiple terms into different cells. It concatenates all cells together and compares them.

 %Load mat files.
 load('model.mat');
 load('dataMtb.mat');
 %Variables of interest.
 Gene     = string(dataMtb.gene);
 grRules  = string(iEK1011.grRules);
 %Expression for regexp to split cells.
 expression = '|(|and|or|)';
 %Split cells containing characters defined in expression.
 grRules = cellfun(@(x) regexp(x,expression,'split'),grRules,'UniformOutput',false);
 %Determine size of cell elements.
 size_cell = cellfun(@(x) size(x,2),grRules,'UniformOutput',false);
 %Reshape cells.
 grRules = cellfun(@(x,y)reshape(x,[y,1]),grRules,size_cell,'UniformOutput',false);
 %Concatenate all cells vertically.
 grRules = vertcat(grRules{:});
 %Eliminate empty cells.
 grRules = grRules(~cellfun('isempty',grRules));
 %Remove unwanted characters.
 grRules = cellfun(@(x) regexprep(x,'[ ,(,)]',''),grRules,'UniformOutput',false);
 %Equal terms.
 equal = grRules(ismember(grRules,Gene));

  3 Comments

Thank you for the detailed code Paolo. When I run the code, I get the error Unbalanced or unexpected parenthesis or bracket at the reshape cells code line. On checking manually, the code looks fine. I even retyped the whole line, but the error persists. Do you have any suggestions to fix this?

Bhushan, there was a really small typo that occured when I was formatting the code for the answer. There's a missing comma,

reshape(x[y,1]) 

is supposed to be

reshape(x,[y,1])

Note the comma after the x. I will edit my answer with the correct version.

Sign in to comment.


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today