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

조회 수: 1 (최근 30일)
Sarah Nasra
Sarah Nasra 2022년 3월 17일
댓글: Sarah Nasra 2022년 3월 17일
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?

답변 (2개)

Voss
Voss 2022년 3월 17일
편집: Voss 2022년 3월 17일
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 2022년 3월 17일
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()

카테고리

Help CenterFile Exchange에서 Software Development Tools에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by