Info
This question is closed. Reopen it to edit or answer.
Creating a matrix of 1's and 0s after comparing strings
2 views (last 30 days)
Show older comments
Hi all,
Ive been having some trouble with my code for some months now. I have several very large files that need to have strings compared, and if theres a match, put a 1 in a box, if false, put a 0 in a box.
My matrices are very large; some are as big as 200 rows by 200000 columns, each needing the check to be made. So far, my experience (and probably wrong programming) have made this an extremely long proecss with the code I have, as it seems to check every single cell with every iteration through my for loops. Im no programmer though, and I only got this code after trying many different things.
Any advice on how to optimise/rejig this would be of huge help, as as it is, its taking far too long to go through each, sometimes literally weeks. Here is an example of each strings being compared, where my code goes through the first 28 chars of the longer string to see if theres a match:
'TCGA-A6-2670-01A-02W-0831-10'
'TCGA-AA-A01Q-01A-01W-A005-10_AAAS_8086_12_53702089_-_C'
Here's the code I use:
[no_patients, no_mutations] = size(matrix);
for row_index = 2:no_patients
for column_index = 2:no_mutations
matrix{row_index,column_index} = strncmp(matrix{row_index,1},matrix{1,column_index},28)
end
end
thanks
0 Comments
Answers (1)
the cyclist
on 24 May 2014
Edited: the cyclist
on 24 May 2014
Here is a miniature version, where I just checked the first 2 characters (N=2) of a fixed string against a cell array. Does this do what you want?
s = 'AB';
matrix = {'ABA','ABB';
'ACA','ACC'}
N = 2;
tf = cellfun(@(x)strncmp(x,s,N),matrix);
I also wrote the result to a separate logical array, rather than storing in-place.
2 Comments
the cyclist
on 24 May 2014
Sorry. I think I misread your code. Can I just clarify? Are you checking ALL of the elements in Row 1 of your matrix and ALL of the elements in Column 1 of your matrix?
So, if matrix(16,1) matches matrix(1,283), then you want to put a 1 into element (16,283)?
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!