Please help me store data from iterations

2 visualizaciones (últimos 30 días)
James
James el 5 de Feb. de 2012
Editada: Matt J el 1 de Oct. de 2013
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

Respuestas (4)

Image Analyst
Image Analyst el 5 de Feb. de 2012
Make tot_hrs1 an array, just like you did for cell_count.
tot_hrs1(i)
  3 comentarios
Image Analyst
Image Analyst el 5 de Feb. de 2012
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 el 5 de Feb. de 2012
Thanks for your comments
Can you see my answer below. This is actually what I am trying to do.
Any suggestions?
Thanks

Iniciar sesión para comentar.


Amit Davidi
Amit Davidi el 5 de Feb. de 2012
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 comentario
James
James el 5 de Feb. de 2012
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

Iniciar sesión para comentar.


James
James el 5 de Feb. de 2012
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 comentarios
Image Analyst
Image Analyst el 5 de Feb. de 2012
Do you mean 1e+9 instead of 1e-9?
James
James el 5 de Feb. de 2012
yes sorry about that.

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 5 de Feb. de 2012
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 comentarios
James
James el 5 de Feb. de 2012
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 el 6 de Feb. de 2012
Also, what is int32?
Thanks

Iniciar sesión para comentar.

Categorías

Más información sobre Graphics Performance en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by