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

조회 수: 1 (최근 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.
  댓글 수: 1
Rasif Ajwad
Rasif Ajwad 2015년 10월 22일
편집: Rasif Ajwad 2015년 10월 22일
Thanks. It worked. :)
But can you please explain this command as I am not familiar with it.

댓글을 달려면 로그인하십시오.

추가 답변 (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