Please help me store data from iterations
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposte (4)
Image Analyst
il 5 Feb 2012
Make tot_hrs1 an array, just like you did for cell_count.
tot_hrs1(i)
3 Commenti
Image Analyst
il 5 Feb 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.
Amit Davidi
il 5 Feb 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
Image Analyst
il 5 Feb 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 Commenti
Vedere anche
Categorie
Scopri di più su Graphics Performance in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!