How to create an array from a while loop

조회 수: 11 (최근 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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by