How to create an array from a while loop

8 visualizaciones (últimos 30 días)
Bryan Morrow
Bryan Morrow el 29 de Mzo. de 2014
Comentada: Umesh Gautam el 16 de En. de 2020
Alright so my professor gave me this prompt, and i normally do well in this class. However, this one totally has me stumped. I'll post the prompt and then my attempt and any advice or criticism would be much appreciated.
3. Experiment shows that bacteria reproduce once every 30 minutes. Assume that the experiment starts with three bacteria. Write a program that calculates the number of bacteria present at intervals of one half hour from the beginning of the experiment until 6 hours have elapsed. Your program must create two arrays namely; time and bact. Array time contains the time values and array bact contains the bacteria count. Plot number of bacteria versus time. Your plot must have axes labels, grid and a title. The title is “your name – Bacteria Growth”. Use while loop.
Here's what i have so far:
clc
clear
time=(0:0.5:6);
b=1.5;
n=0;
while n<13
b=b*2;
n=(n)+1;
fprintf('\n%2d',b);
end
bact=b(1:12);
fprintf('\n %5d',bact);
I am aware that i do have to graph this, i can do that with ease, i just want to make sure im producing the right numbers and the two appropriate arrays. The problem appears to arise when i try to draw upon the b values from the loop. Am i not able to do this?
Thank you.

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 29 de Mzo. de 2014
Editada: Azzi Abdelmalek el 29 de Mzo. de 2014
clc
clear
time=(0:0.5:6);
b=1.5;
bact=zeros(1,12);
n=1;
while n<13
b=b*2;
bact(n)=b;
n=(n)+1;
fprintf('\n%2d',b);
end
bact
  2 comentarios
Bryan Morrow
Bryan Morrow el 29 de Mzo. de 2014
Much thanks
Umesh Gautam
Umesh Gautam el 16 de En. de 2020
Thanks

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by