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

2 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Guillaume
Guillaume am 22 Okt. 2015
Bearbeitet: Guillaume am 22 Okt. 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.
  1 Kommentar
Rasif Ajwad
Rasif Ajwad am 22 Okt. 2015
Bearbeitet: Rasif Ajwad am 22 Okt. 2015
Thanks. It worked. :)
But can you please explain this command as I am not familiar with it.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by