How to copy specific columns of a character array to another character array?
2 views (last 30 days)
Show older comments
I have a character array named 'seq' as following:
MRSLMVLALLAVAALCLCLAGPADAKPSSAESRKGGATFVSKREGSEVVRRLRRYLDSGL
MRTPMLLALLALAT--LCLAGRADAKPGDAESGK-GAAFVSKQEGSEVVKRLRRYLDHWL
MRALTLLALLALAT--LCITGQAGAKPSGAESSK-GAAFVSKQEGSEVVKRPRRYLYQWL
MRALTLLALLALAA--LCIAGQAGAKPSGAESSK-GAAFVSKQEGSEVVKRPRRYLYQWL
MRALTLLALLALAA--LCIAGQAGAKPSGAESSK-GAAFVSKQEGSEVVKRPRRYLYQWL
I want to remove those columns which has more than 50% gaps (-) in them. For example, column 15 has 4 gaps out of 5, so I want to remove that column. After removing these columns, I want to take the rest to another character array (e.g 'mod_seq').
I tried with this code:
[rowLen, colLen] = size(seq);
count = 0;
mod_col_no = 1;
mod_seq = zeros(rowLen,colLen);
end
for i=1:colLen
for j=1:rowLen
if seq(j,i) == '-'
count = count+1;
end
end
if count > ceil(rowLen/2)
continue;
else
mod_seq(:,mod_col_no) = seq(:,i);
mod_col_no=+1;
end
end
mod_seq = char(mod_seq);
I have tried like this because I cannot initialize 'mod_seq'. I can't simply make the two arrays equal because when I delete columns, mod_seq will have less dimension.
0 Comments
Accepted Answer
Guillaume
on 22 Oct 2015
Edited: Guillaume
on 22 Oct 2015
The way to fix your loop would be to gather the list of columns to delete inside the loop and perform the deletion after the loop.
Even simpler is to not bother with loops at all when a single line of code can do the same:
seq=['MRSLMVLALLAVAALCLCLAGPADAKPSSAESRKGGATFVSKREGSEVVRRLRRYLDSGL';
'MRTPMLLALLALAT--LCLAGRADAKPGDAESGK-GAAFVSKQEGSEVVKRLRRYLDHWL';
'MRALTLLALLALAT--LCITGQAGAKPSGAESSK-GAAFVSKQEGSEVVKRPRRYLYQWL';
'MRALTLLALLALAA--LCIAGQAGAKPSGAESSK-GAAFVSKQEGSEVVKRPRRYLYQWL';
'MRALTLLALLALAA--LCIAGQAGAKPSGAESSK-GAAFVSKQEGSEVVKRPRRYLYQWL';]
mod_seq = seq(:, sum(seq == '-') < size(seq, 1)/2)
sum(seq == '-') gives you straight away the count of '-' per column.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!