How to create an array from a while loop

8 ビュー (過去 30 日間)
Bryan Morrow
Bryan Morrow 2014 年 3 月 29 日
コメント済み: Umesh Gautam 2020 年 1 月 16 日
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.

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 3 月 29 日
編集済み: Azzi Abdelmalek 2014 年 3 月 29 日
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 件のコメント
Bryan Morrow
Bryan Morrow 2014 年 3 月 29 日
Much thanks
Umesh Gautam
Umesh Gautam 2020 年 1 月 16 日
Thanks

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by