필터 지우기
필터 지우기

Please help me store data from iterations

조회 수: 1 (최근 30일)
James
James 2012년 2월 5일
편집: Matt J 2013년 10월 1일
How can I store the values of cell_count and tot_hrs1 from each iteration and then plot them against each other.
n = [-1:32];
%n=32;
%n=input('counts, n= ')
cell_count=1;
tot_hrs1=0;
for i=0:n
r=0:7;
t=3+4*r;
%r=1:(70/8);
%multiples_8=8.*r;
if i~=t(1)
cell_count(i)=cell_count(i)*0.90+cell_count(i);
%end
elseif i==t(1)
cell_count(i)=cell_count(i)*0.90+cell_count(i)-1;
end
if n < 0
tot_hrs1=0;
elseif n >= 0
tot_hrs1=tot_hrs1+2;
end
end
tot_hrs1
format('shortE')
cell_count(i)
Thanks

답변 (4개)

Image Analyst
Image Analyst 2012년 2월 5일
Make tot_hrs1 an array, just like you did for cell_count.
tot_hrs1(i)
  댓글 수: 3
Image Analyst
Image Analyst 2012년 2월 5일
No, it will probably only take the first one, which may not even enter the loop. Don't use i as the loop iterator - i is the imaginary variable - use something else, like k. You could do
for k = n
in which case k will take on all values in n, but then you can't do something like tot_hrs1(k) because indexes must be integers starting at 1 - no 0 or -1 allowed.
Finally, try using descriptive variable names, for example the descriptive "totalHours" rather than the cryptic "tot_hrs1." It will make it easier for people to follow and understand your code.
James
James 2012년 2월 5일
Thanks for your comments
Can you see my answer below. This is actually what I am trying to do.
Any suggestions?
Thanks

댓글을 달려면 로그인하십시오.


Amit Davidi
Amit Davidi 2012년 2월 5일
Hello James,
If you your loop to run n times, use:
n = 32;
for i = 1 : n
...
end
An array in Matlab start from the index 1, so your loop need to start from 1 (not 0). Also, variable like r and t, could be calculated once before the loop. It's unnecessary and wastes time. To further improve running time, pre-allocate memory to arrays (assuming you know their size). I've tried to rewrite your code, although I'm not sure I understood the purpose of your code:
n = 4;
cell_count = ones(1,n);
tot_hrs1 = zeros(1,n);
r = 0 : 7;
t = 3 + 4 * r;
for i = 1 : n
if i ~= t(1)
cell_count(i) = cell_count(1) * 0.90 + cell_count(1);
elseif i==t(1)
cell_count(i) = cell_count(1) * 0.90 + cell_count(1) - 1;
end
if n < 0
tot_hrs1(i) = 0;
elseif n >= 0
tot_hrs1(i) = tot_hrs1(1) + 2;
end
end
tot_hrs1
cell_count
plot(tot_hrs1, cell_count, 'o-')
PS - a cosmetic remark, if I may: I would recommend you to use more spaces in your codes, both in between lines (as I did above), it'll make your codes much more readable.
Good luck, Amit
  댓글 수: 1
James
James 2012년 2월 5일
Thanks for your comments.
Can you see my answer below. That is what I am trying to do.
with your method it is not actually counting.
Thanks

댓글을 달려면 로그인하십시오.


James
James 2012년 2월 5일
I am wanting to do the following: Calculate the total hours to reach a cell count of 1e-9 The parameters: Each cell divides 1 every 2 hours Each cell has a 10% chance of dying each time it divides The rate of cell loss is 1 every 8 hours
Time Cell Count
x0 1 Begin with 1 cell
x2 x0*0.90+x0
x4 x2*0.90+x2
x6 x4*0.90+x4
x8 x6*0.90+x6-1 Loss of a cell after 8 hours
Then plot the total hours vs the cell count
Thanks
  댓글 수: 4
Image Analyst
Image Analyst 2012년 2월 5일
Do you mean 1e+9 instead of 1e-9?
James
James 2012년 2월 5일
yes sorry about that.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2012년 2월 5일
James, Since you made an attempt at it, I improved (actually totally rewrote it) for you. Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Determine some starting number of cells.
% It must be greater than 1 since 10 percent die
% on the first iteration.
cell_count=10;
totalHours=0;
index = 1;
while cell_count(index) < 1e+9
% Increment the generation index.
index = index + 1
% Determine and save the hours since we started.
totalHours(index) = totalHours(index-1) + 2;
% 10 percent of the cells dies at this iteration
% before they can divide.
cell_count(index) = int32(0.9 * cell_count(index-1));
if cell_count(index) < 1
% No more cells left so bail out.
break;
end
% Multiply the remaining cells by two.
% Now cell_count(index) is the remaining number of cells
% so we don't use the -1 anymore.
% I guess in biology multiply and divide mean the same thing.
cell_count(index) = cell_count(index) * 2
end
totalHours
cell_count
% Plot linearly.
subplot(1,2,1);
plot(totalHours, cell_count);
grid on;
xlabel('Time [hours]', 'FontSize', fontSize);
ylabel('Cell Count', 'FontSize', fontSize);
title('Cell Count vs. Time', 'FontSize', fontSize);
% Plot on a semilog plot
subplot(1,2, 2);
semilogy(totalHours, cell_count);
grid on;
xlabel('Time [hours]', 'FontSize', fontSize);
ylabel('Cell Count', 'FontSize', fontSize);
title('Cell Count vs. Time', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
  댓글 수: 3
James
James 2012년 2월 5일
So at 0 hours you start with 1 cell
Then at 2 hours you have 1.9 cells (which is your previous cell count*1.9)
Then at 4 hours you have 3.61 cells (which is your previous cell count*1.9)
Then at 6 hours you have 6.859 cells (which is your previous cell count*1.9)
Then at 8 hours you have 12.0321 cells (which is your previous cell count*1.9 - 1)
This should just repeat until you reach 66 hours and get 1.4595e+9 cell count.
Thanks
James
James 2012년 2월 6일
Also, what is int32?
Thanks

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Instrument Control Toolbox Supported Hardware에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by