how can i count the number of times that the symbol appears?

2 views (last 30 days)
seq1 = ["AQSVPWGISRVQAPAAHNRGLTGSGVKVAVLDTGISTHPDLNIRGGASFVPGEPSTQDGN"+...
"GHGTHVAGTIAALNNSIGVLGVAPSAELYAVKVLGASGSGSVSSIAQGLEWAGNNGMHVA"+...
"NLSLGSPSPSATLEQAVNSATSRGVLVVAASGNSGAGSISYPARYANAMAVGATDQNNNR"+...
"ASFSQYGAGLDIVAPGVNVQSTYPGSTYASLNGTSMATPHVAGAAALVKQKNPSWSNVQI"+...
"RNHLKNTATSLGSTNLYGSGLVNAEAATR"];
seq2 = ["MRQSLKVMVLSTVALLFMANPAAASEEKKEYLIVVEPEEVSAQSVEESYDVDVIHEFEEI"+...
"PVIHAELTKKELKKLKKDPNVKAIEKNAEVTISQTVPWGISFINTQQAHNRGIFGNGARV"+...
"AVLDTGIASHPDLRIAGGASFISSEPSYHDNNGHGTHVAGTIAALNNSIGVLGVAPSADL"+...
"YAVKVLDRNGSGSLASVAQGIEWAINNNMHIINMSLGSTSGSSTLELAVNRANNAGILLV"+...
"GAAGNTGRQGVNYPARYSGVMAVAAVDQNGQRASFSTYGPEIEISAPGVNVNSTYTGNRY"+...
"VSLSGTSMATPHVAGVAALVKSRYPSYTNNQIRQRINQTATYLGSPSLYGNGLVHAGRATQ"];
[Score_global, Alignment_global] = nwalign(seq1,seq2,ScoringMatrix="blosum62");
for i=1:361
cont = 0;
if Alignment_global(2,i) == '|' || ':'
cont=cont+1;
else
cont=cont
end
end
i have the alignment_global and i want to count how much times i have ":" and "|" in the second row.
i did a for loop that pass on the whole second row!!
but i get that cont=1!!!
why the loop didnt count all the times!!
can u help me?

Answers (2)

Voss
Voss on 17 Mar 2022
Edited: Voss on 17 Mar 2022
One thing to do is not to reset the count to zero each time through the for loop. See modified code (including other modifications) below:
seq1 = ["AQSVPWGISRVQAPAAHNRGLTGSGVKVAVLDTGISTHPDLNIRGGASFVPGEPSTQDGN"+...
"GHGTHVAGTIAALNNSIGVLGVAPSAELYAVKVLGASGSGSVSSIAQGLEWAGNNGMHVA"+...
"NLSLGSPSPSATLEQAVNSATSRGVLVVAASGNSGAGSISYPARYANAMAVGATDQNNNR"+...
"ASFSQYGAGLDIVAPGVNVQSTYPGSTYASLNGTSMATPHVAGAAALVKQKNPSWSNVQI"+...
"RNHLKNTATSLGSTNLYGSGLVNAEAATR"];
seq2 = ["MRQSLKVMVLSTVALLFMANPAAASEEKKEYLIVVEPEEVSAQSVEESYDVDVIHEFEEI"+...
"PVIHAELTKKELKKLKKDPNVKAIEKNAEVTISQTVPWGISFINTQQAHNRGIFGNGARV"+...
"AVLDTGIASHPDLRIAGGASFISSEPSYHDNNGHGTHVAGTIAALNNSIGVLGVAPSADL"+...
"YAVKVLDRNGSGSLASVAQGIEWAINNNMHIINMSLGSTSGSSTLELAVNRANNAGILLV"+...
"GAAGNTGRQGVNYPARYSGVMAVAAVDQNGQRASFSTYGPEIEISAPGVNVNSTYTGNRY"+...
"VSLSGTSMATPHVAGVAALVKSRYPSYTNNQIRQRINQTATYLGSPSLYGNGLVHAGRATQ"];
[Score_global, Alignment_global] = nwalign(seq1,seq2,ScoringMatrix="blosum62");
cont = 0;
for i=1:361
if Alignment_global(2,i) == '|' || Alignment_global(2,i) == ':'
cont=cont+1;
end
end
disp(cont);
241

Walter Roberson
Walter Roberson on 17 Mar 2022
You initialize your counter in each iteration. After any one iteration the counter will be either 0 or 1, but either way the next iteration you reset to 0. At the end of the loop the counter reflects only the last test.
MATLAB does not offer a way to test for multiple values using the syntax
value == constant1 || constant2
MATLAB interprets that as
(value == constant1) || (constant2)
which is two completely different logical tests. In MATLAB the evaluation of a value or constant as a logical condition, without a relationship operator, is completely valid, and is equivalent to testing that the value is non-zero. The constant ':' is non-zero so your code is equivalent to
(value == constant1) || true
and no matter what the value test came out as, the overall result is going to be true
I suggest that you consider using ismember()

Tags

Community Treasure Hunt

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

Start Hunting!