How to copy specific columns of a character array to another character array?

3 次查看(过去 30 天)
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.

采纳的回答

Guillaume
Guillaume 2015-10-22
编辑:Guillaume 2015-10-22
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by