How can I create a for loop to count the number of letters in my array?

I have the following array:
bases = [ t t g c g c a t g c g c a c a ]
I want to create a for loop that loops through the array and counts the number of g’s within it.
do i use the if function?

 Respuesta aceptada

madhan ravi
madhan ravi el 20 de Nov. de 2018
Editada: madhan ravi el 20 de Nov. de 2018
Stephen's way is efficient but if you insist loop then:
bases = 't t g c g c a t g c g c a c a'
ctr=0;
for i = 1:numel(bases)
if bases(i)=='g'
ctr=ctr+1;
end
end
number_of_gs=ctr

16 comentarios

thank you!
if i want to count a different letter, can i do this with a while loop from the for loop?
EDITED
bases = 't t g c g c a t g c g c a c a'
ctr=0;
ctr1=0;
i=1;
for i=1:numel(bases)
if bases(i)=='t'
ctr=ctr+1;
elseif bases(i)=='g'
ctr1=ctr1+1;
end
end
number_of_ts=ctr
number_of_gs=ctr1
one more question sorry!
i want to include the count for 2 letters in one for loop? how can i do so?
could you put this in a for format if possible?
it doesnt seem to provide the correct answer when run?
thanks very much for your help madhan
You are welcome :)
is there a way i can use that for loop to calculate the ratio of G-C’s rather than g-t’s?
you can use the same way for those
ive replaced the if primer with
if primer(i)==gc
and this hasnt resulted in the answer of 5?
Not clear what's primer and what why should it be 5 explain clearly
Stephen23
Stephen23 el 20 de Nov. de 2018
Editada: Stephen23 el 20 de Nov. de 2018
@Fope Ayo: note that primer(i) refers to one element of primer (i.e. one character), so you are testing if two different characters (i.e. g and c) are both equal to the same character. Clearly this will never be true.
Note that my answer already shows you how to count how many times a specific substirng occurs.
@madhan ravi sorry i meant bases
Basically I am trying to use the for loop created which counts the number of two elements e.g g’s and c’s, then use this for loop to calculate the GC ratio for the sequence
"Basically I am trying to use the for loop created which counts the number of two elements e.g g’s and c’s, then use this for loop to calculate the GC ratio for the sequence"
So is your aim to play around with loops, or is your aim to get answers from your code?

Iniciar sesión para comentar.

Más respuestas (1)

Stephen23
Stephen23 el 20 de Nov. de 2018
Editada: Stephen23 el 20 de Nov. de 2018
The simple MATLAB solution:
>> V = 'ttgcgcatgcgcaca';
>> nnz(V=='g')
ans = 4
Or to count occurances of all of the different letters:
>> U = unique(V)
U = 'acgt'
>> histc(V,U)
ans =
3 5 4 3
I.e. 'a' occurs three times, 'c' occurs five times, 'g' occurs four times, and 't' occurs three times.
To count how many times a specific substring occurs:
>> numel(strfind(V,'gc'))
ans = 4
I.e. the substring 'gc' occurs four times.

7 comentarios

thanks for this but i need to relate it to my for loop
Stephen23
Stephen23 el 20 de Nov. de 2018
Editada: Stephen23 el 20 de Nov. de 2018
"thanks for this but i need to relate it to my for loop"
This is MATLAB, so why bother using a for loop?
":...then use this for loop to calculate the GC ratio for the sequence"
You are wasting your time using loops:
>> V = 'ttgcgcatgcgcaca';
>> nnz(V=='g') / nnz(V=='c')
ans = 0.80000
i know this is matlab.
I’m using the for loop that madhan created.
It’s what I need for my subscript, I don’t belive i’m wasting my time but thanks for you help
Thanks for your input Stephen.
why are you deleting and editing your comments Stephen?
Stephen23
Stephen23 el 20 de Nov. de 2018
Editada: Stephen23 el 20 de Nov. de 2018
I often edit my comments for clarity and spelling. Some comments and answers that turn out to be superfluous to the discussion I will also remove.
I notice that you have opened a new question on this topic here:
which is clearer describing what you are actually trying to achieve. You should put a link back to this thread, so that readers of that new thread know what you have already tried and what information you have been given. Otherwise people often just give you the same solutions, which is unsatisfying for everyone involved.
Once again, thanks for your insight and input.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Versión

R2018b

Preguntada:

el 20 de Nov. de 2018

Comentada:

el 20 de Nov. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by