Please help me store data from iterations

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

Answers (4)

Make tot_hrs1 an array, just like you did for cell_count.
tot_hrs1(i)

3 Comments

I did that and it is only storing the first iteration, not the next values. Also do you think my counter i=0:n is correct as n is an array and I don't think that it is stepping through the array n.
Thanks
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.
Thanks for your comments
Can you see my answer below. This is actually what I am trying to do.
Any suggestions?
Thanks

Sign in to comment.

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 Comment

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

Sign in to comment.

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 Comments

How can you have less than 1 cell? A cell is either there and alive, or it's dead. How can you have 10e-9 of a cell? It looks like your cell count is cellCount(k) = (k-1)*1.9 except for iteration 5 (time x8). And it doesn't seem like a valid Monte Carlo approach since you're not using the rand() function at all to get a random number that gets the possibility that the cell dies. Something like
if rand(1) < 0.1
% Cell dies
else
% Cell lives
end
So I have one cancer tumor of a particular size. Inside of that tumor there are 1e-9 cancer cells. There are some calculations that I did to figure out how many cancer cells are in a tumor of 1cm size if the cancer cells are 10micron size. This is not that important thought to my actually Matlab question. I want to find out how many hours it takes to reach 1e-9 cancer cells. So starting out at time 0 the cell count is 1. Then in 2 hours you have a division, but at every division there is a 10% chance of the cell dying. Then at every 8 hours one of those cells dies. This is just a hypothetical case.
Thanks
Do you mean 1e+9 instead of 1e-9?
yes sorry about that.

Sign in to comment.

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 Comments

Thanks for your suggestions. I'm not sure what your cell_count=10; in the beginning means. The starting cell_count has to be 1.
I know that it takes a total of 66 hrs to reach a cell count of 1.4595e+9
I am having a hard time getting this with your code. Any insight?
Thanks
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
Also, what is int32?
Thanks

Sign in to comment.

Categories

Find more on Parallel Computing Toolbox in Help Center and File Exchange

Asked:

on 5 Feb 2012

Edited:

on 1 Oct 2013

Community Treasure Hunt

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

Start Hunting!